Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 3 如何在ASP.net MVC中将关系数据绑定到模型?_Asp.net Mvc 3 - Fatal编程技术网

Asp.net mvc 3 如何在ASP.net MVC中将关系数据绑定到模型?

Asp.net mvc 3 如何在ASP.net MVC中将关系数据绑定到模型?,asp.net-mvc-3,Asp.net Mvc 3,我正在尝试为ASP.net MVC 3中的对象制作一个编辑器。它看起来像这样: <div class="editor-label"> @Html.LabelFor(model => model.Name) </div> <div class="editor-field"> @Html.EditorFor(model => model.Name) @Html.ValidationMessageFor(model => m

我正在尝试为ASP.net MVC 3中的对象制作一个编辑器。它看起来像这样:

<div class="editor-label">
    @Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.Name)
    @Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
    @Html.LabelFor(model => model.foo)
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.foo)
    @Html.ValidationMessageFor(model => model.foo)
</div>
@if (Model.Items.Count > 0)
{
<table>          
    @foreach (var ii in Model.Items)
    { @Html.EditorFor(item => ii) }
</table>
}

@LabelFor(model=>model.Name)
@EditorFor(model=>model.Name)
@Html.ValidationMessageFor(model=>model.Name)
@LabelFor(model=>model.foo)
@EditorFor(model=>model.foo)
@Html.ValidationMessageFor(model=>model.foo)
@如果(Model.Items.Count>0)
{
@foreach(模型项中的var ii)
{@Html.EditorFor(item=>ii)}
}
在本例中,Items是另一种对象的列表。问题是,当模型从视图中编辑后发回时,数据将更改为model.Items,而数据将更改为Name和foo。如何使项目的数据正确绑定?

模型类:

public class HomeControllerModel
{
    public string Name { get; set; }
    public string foo { get; set; }
    public List<string> Items { get; set; }

    public HomeControllerModel()
    {
        Items = new List<string>();
    }
}
视图:

@使用MVCAPApplication4.控制器
@模型HomeControllerModel
@{
布局=空;
}
指数
@LabelFor(model=>model.Name)
@EditorFor(model=>model.Name)
@Html.ValidationMessageFor(model=>model.Name)
@LabelFor(model=>model.foo)
@EditorFor(model=>model.foo)
@Html.ValidationMessageFor(model=>model.foo)
@EditorFor(model=>model.Items)

你不必反复浏览
项目

啊哈,就是这样!非常感谢。如果您希望能够在“编辑”视图的列表中添加更多项目,该怎么办?我怎么做?
public class HomeController : Controller
{


    [HttpGet]
    public ActionResult Index()
    {
        var model = new HomeControllerModel();
        model.Name = "LukLed";
        model.foo = "bar";
        model.Items.Add("AAA");
        model.Items.Add("BBB");
        model.Items.Add("CCC");
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(HomeControllerModel model)
    {
        return View(model);
    }
@using MvcApplication4.Controllers
@model HomeControllerModel
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <title>Index</title>
</head>
<body>
    <div>
        <form action="/Home/Index" method="post">
        <div class="editor-label">
            @Html.LabelFor(model => model.Name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Name)
            @Html.ValidationMessageFor(model => model.Name)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.foo)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.foo)
            @Html.ValidationMessageFor(model => model.foo)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Items)
        </div>
        <input type="submit" value="Submit" />
        </form>
    </div>
</body>
</html>