[英]Return List<T> from view to controller in MVC 3
I currently have an object Tag
defined as follows: 我目前有一个对象
Tag
定义如下:
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? 如何通过我的模型的IList集合返回我的复选框组(在注释中指定)中的所选项?
Use Editor Templates 使用编辑器模板
For having the Checkbox, Add another Proeprty to your Tag
classs to specify whether it is selected or not. 要获得Checkbox,请在
Tag
类中添加另一个Proeprty,以指定是否选中它。
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. 现在,从您的
GET
操作,您可以在模型的Tags
属性中设置标签列表并将其发送到视图。
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
). 现在让我们创建一个编辑模板,转到
View/YourControllerName
并创建一个名为EditorTemaplates
的文件夹,并在那里创建一个与Property type( Tag.cshtml
)同名的新视图。
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. 现在,在主视图中,使用
EditorFor
Html Helper方法调用编辑器模板。
@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. 现在,当您发布表单时,您的模型将具有标签集合,其中选定的复选框将具有
IsSelected
属性的True值。
[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 像这样
This is similar to something i have done in a site im working on. 这类似于我在网站上所做的事情。
I used this extension @Html.CheckBoxListFor() 我使用了这个扩展名@ Html.CheckBoxListFor()
Hope this helps. 希望这可以帮助。
If you can add a bool IsChecked property to your Tag model then you can just use EditorFor (or CheckBoxFor) in a loop. 如果你可以在你的Tag模型中添加一个bool IsChecked属性,那么你可以在循环中使用EditorFor(或CheckBoxFor)。 The trick is to use a for loop with indexer (not foreach) such that you access the property via the views main model.
诀窍是使用带索引器的for循环(不是foreach),以便通过视图主模型访问属性。 Then the modelbinder will do the rest for you so your POST action will receive MyModel with its Tags IsChecked properties set to the correct states.
然后,模型绑定器将为您完成剩下的工作,因此您的POST操作将接收MyModel,其标签IsChecked属性设置为正确的状态。
Models: 楷模:
public class Tag
{
public string Name { get; set; }
public bool IsChecked { get; set; }
}
public class MyModel
{
public string Name { get; set; }
public List<Tag> Tags { get; set; }
}
The View: 风景:
@model MyMvcApplication.Models.MyModel
@using (Html.BeginForm())
{
<div>
@Html.LabelFor(m => m.Name)
@Html.TextBoxFor(m => m.Name)
</div>
<div>
@for (int i = 0; i < Model.Tags.Count; i++)
{
@Html.DisplayFor(x => Model.Tags[i].Name)
@Html.EditorFor(x => Model.Tags[i].IsChecked)
}
</div>
<input type="submit" value="Submit" />
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.