Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.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
C# 在MVC中发布多个对象列表_C#_Asp.net Mvc - Fatal编程技术网

C# 在MVC中发布多个对象列表

C# 在MVC中发布多个对象列表,c#,asp.net-mvc,C#,Asp.net Mvc,我可以提交一个包含两组对象的表单吗?我的模型如下所示 public class Item { public int Id{get;set;} public bool Selected{get;set;} public string Name {get;set;} public string Price {get;set;} } public class Translations {

我可以提交一个包含两组对象的表单吗?我的模型如下所示

        public class Item 
    {

    public int Id{get;set;}
            public bool Selected{get;set;}
    public string Name {get;set;}
    public string Price {get;set;}

    }

    public class Translations
    {

    public int Id{get;set;}
            public bool Selected{get;set;}
    public int ItemId{get;set;}
    public string Name {get;set;}
    public string TransName {get;set;}

    }

    public class ModelToSubmit
    {
    public List<Item> Items{get;set;}
    public List<Translations> TransItems{get;set;}
    }
公共类项目
{
公共int Id{get;set;}
已选择公共布尔值{get;set;}
公共字符串名称{get;set;}
公共字符串Price{get;set;}
}
公共类翻译
{
公共int Id{get;set;}
已选择公共布尔值{get;set;}
公共int ItemId{get;set;}
公共字符串名称{get;set;}
公共字符串TransName{get;set;}
}
公共类模型提交
{
公共列表项{get;set;}
公共列表传输项{get;set;}
}

如何将模型提交给控制器?有什么想法吗?{要从ModelToSubmit的项和传输项中获取所有选定的项,MVC使用html元素的name属性将视图html控件绑定到提交给控制器的模型,如果是列表,name属性将采用以下格式:

<input type='text' name='Items[0].Id'/>
<input type='text' name='Items[0].Name'/>
<input type='text' name='Items[0].Price'/>
.
.
.
<input type='text' name='Items[n].Id'/>
<input type='text' name='Items[n].Name'/>
<input type='text' name='Items[n].Price'/>

.
.
.
对于过渡期

<input type='text' name='TransItems[0].Id'/>
<input type='text' name='TransItems[0].Name'/>
<input type='text' name='TransItems[0].Price'/>
.
.
.
<input type='text' name='TransItems[m].Id'/>
<input type='text' name='TransItems[m].Name'/>
<input type='text' name='TransItems[m].Price'/>

.
.
.
因此,您需要确保与Items和transitem相关的html控件的name属性具有该格式

希望能有所帮助。

这是您的主要观点

@model MVC3Stack.Models.ModelToSubmit
@{
    ViewBag.Title = "Create";
}
@using (Html.BeginForm("Index1","Home", FormMethod.Post))
{
    Html.RenderPartial("ItemsPartial", Model.Items);
    Html.RenderPartial("TranslationPartial", Model.TransItems);
    <input type="submit" value="Post" />
}
样本输出

以及在动作方法中接收的数据

最简单的方法是使用EditorTemplates。他们为您处理所有的艰苦工作


我希望您了解MVC ModelBinding和Stringly-Typed视图,如果不了解,请先熟悉它们

MVCModelbinding使用html标记的name属性将其与相应的实体属性相匹配

稍微修改实体以更好地理解

实体

public class ModelToSubmit
   {
    public string TestProp{get; set;}
    public List<Item> Items{get;set;}
    public List<Translations> TransItems{get;set;}
   }

以下是您如何了解自己的方法:按照您计划发送到服务器的格式进行编码,查看它的格式,并以相同的格式发送。您必须创建两个部分视图。其中一个用于“项目”,另一个用于“翻译”。根据列表的计数,部分视图将在您的页面中呈现/调用。提交时将显示一个自动填充“ModelToSubmit”类,您将在控制器中获得更新的值。您是否可以为其提供一些虚拟数据的示例?因为使用局部视图可以提交数据?EditorTemplates为您完成大部分工作…这就是它们的设计目的。如果我的模型不包含单个列表,会发生什么?EditorFor将如何处理在这种情况下有什么帮助?@Nilesh-我的例子有两个列表。所以我不太理解你的问题。我的问题是,如果两个列表都是空的,会发生什么?当我使用
EditorFor
时,两个列表都是空的,没有控件被呈现。对此不确定,可能是我做错了什么!@Nilesh-是的,当然不会呈现任何内容。。。如果要渲染某些内容,应添加一个空白项。这正是我在回答中所做的。添加了多个空白项。
@{
    Layout = null;
}
<table>
    @for (int i = 0; i < 5; i++)
    {
        <tr>
            <td>
                <span>Name</span> @Html.TextBox("TransItems[" + i + "].Name")
            </td>
            <td>
                <span>Trans Name</span> @Html.TextBox("TransItems[" + i + "].TransName")
            </td>
            <td>
                <span>Selected</span>@Html.CheckBox("TransItems[" + i + "].Selected")
            </td>
        </tr>
    }
</table>
[HttpPost]
public ActionResult Index1(ModelToSubmit submitModel)
{
   return Json("Index Success", JsonRequestBehavior.AllowGet);
}
@model MVC3Stack.Models.ModelToSubmit

@using (Html.BeginForm("Index"))
{
    <table>
    Html.EditorFor(Model.Items);
    </table>
    <table>
    Html.EditorFor(Model.TransItems);
    </table>
    <input type="submit"/>
}
@model Item

<tr>
    <td>@DisplayFor(x => x.Id)</td>
    <td>@EditorFor(x => x.Name)</td>
    <td>@EditorFor(x => x.Price)</td>
    <td>@CheckBoxFor(x => x.Selected)</td>
</tr>
@model Translations

<tr>
    <td>@Html.DisplayFor(x => x.Id)</td>
    <td>@Html.EditorFor(x => x.ItemId)</td>
    <td>@Html.EditorFor(x => x.Name)</td>
    <td>@Html.EditorFor(x => x.TransName)</td>
    <td>@CheckBoxFor(x => x.Selected)</td>
</tr>
[HttpPost]
public ActionResult Index(ModelToSubmit submitModel)
{
   return View(submitModel);
}
public class ModelToSubmit
   {
    public string TestProp{get; set;}
    public List<Item> Items{get;set;}
    public List<Translations> TransItems{get;set;}
   }
@model MVC3Stack.Models.ModelToSubmit

@using (Html.BeginForm("PostToActionName","PostToControllerName"))
{
    @Html.TextBoxFor(model => model.TestProp)
    @Html.TextBox("Items[0].ItemPropName")
    @Html.TextBox("TransItems[0].TranslationsPropName")
    <input type="submit" value="Post" />
}
[HttpPost]
public ActionResult PostToActionName(ModelToSubmit collection)
{
   return View();
}