Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
143 views
in Technique[技术] by (71.8m points)

c# - Return List<T> from view to controller in MVC 3

I currently have an object Tag defined as follows:

public class Tag
{
    public string Name { get; set; }
}

Now, this is a collection property of a Model which I'm defining as:

public class MyModel
{
    public string Name { get; set; }
    public IList<Tag> Tags { get; set; }
}

In my view I have the following code:

@using (Html.BeginForm())
{
    <div>
        @Html.LabelFor(m => m.Name)
        @Html.TextBoxFor(m => m.Name)
    </div>

    <div>
        <!--
        Here I'd like a collection of checkbox inputs, where the selected names
        get passed back to my controller via the IList<Tag> collection
        -->
    </div>

    <input type="submit" value="Submit" />
}

How do I return the selected items on my checkbox group (specified in comments) via the IList collection of my model?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use Editor Templates

For having the Checkbox, Add another Proeprty to your Tag classs to specify whether it is selected or not.

public class Tag
{
    public string Name { get; set; }
    public bool IsSelected { set; get; }
}

Now from your GET Action, you can set a List of Tags in your Model's Tags Property and sent it to the View.

public ActionResult AddTag()
{
    var vm = new MyModel();

    //The below code is hardcoded for demo. you mat replace with DB data.
    vm.Tags.Add(new Tag { Name = "Test1" });
    vm.Tags.Add(new Tag { Name = "Test2" });

    return View(vm);
}

Now Let's create an Editor Template, Go to The View/YourControllerName and Create a Folder called EditorTemaplates and Create a new View there with the same name as of the Property type ( Tag.cshtml).

enter image description here

Add this content to the new editor template now.

@model Tag
<p>
  <b>@Model.Name</b>   :
  @Html.CheckBoxFor(x => x.IsSelected) <br />
  @Html.HiddenFor(x=>x.Name)
</p>

Now in your Main View, Call your Editor template using the EditorFor Html Helper method.

@model MyModel
<h2>AddTag</h2>
@using (Html.BeginForm())
{
    <div>
        @Html.LabelFor(m => m.Name)
        @Html.TextBoxFor(m => m.Name)
    </div>    
    <div>  
      @Html.EditorFor(m=>m.Tags)         
    </div>    
    <input type="submit" value="Submit" />
}

Now when You Post the Form, Your Model will have the Tags Collection where the Selected Checkboxes will be having a True value for the IsSelected Property.

 [HttpPost]
public ActionResult AddTag(MyModel model)
{
   if(ModelState.IsValid)
   {
      //Check for model.Tags collection and Each items IsSelected property value.
      //Save and Redirect(PRG pattern)
   }
   return View(model);
}

Like this

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...