C# 如何在ASP.NET MVC3中发布部分视图中的数据?

C# 如何在ASP.NET MVC3中发布部分视图中的数据?,c#,asp.net-mvc,asp.net-mvc-3,razor,partial-views,C#,Asp.net Mvc,Asp.net Mvc 3,Razor,Partial Views,我有一个使用viewmodel的视图。 在这个视图中,我在@中使用(Html.BeginForm())块代码,如下所示 @foreach (var party in Model.Participants) { Html.RenderPartial("BlankEditorRow", party); } 局部视图有一些文本框字段,用户可以在这些字段中输入数据。 现在提交按钮并没有放在局部视图

我有一个使用viewmodel的视图。 在这个视图中,我在
@中使用(Html.BeginForm())
块代码,如下所示

 @foreach (var party in Model.Participants)
                {
                    Html.RenderPartial("BlankEditorRow", party);
                }
局部视图有一些文本框字段,用户可以在这些字段中输入数据。 现在提交按钮并没有放在局部视图中,而是放在主视图中

在我的视图中,当我单击submit按钮时,我在viewmodel中得到空值

[HttpPost]
    public ActionResult ActionName(ViewModel model)
    {

    }
我不确定如何从局部视图获取post数据。有人能帮我理解如何从局部视图发布数据吗?举个例子会有很大帮助

编辑:局部视图如下所示:

@model ASPNETMVCApplication.Models.Administration.Account.PartyModel
@使用HtmlHelpers.BeginCollectionItem

<tr class="editorRow">        
    @using (Html.BeginCollectionItem("party"))
    {

        <td>@Html.TextBoxFor(m => m.FirstName, new { @style = "width: 100px;" })
        </td>
        <td>@Html.TextBoxFor(m => m.LastName, new { @style = "width: 100px;" })
        </td>
        <td>
            @Html.DropDownListFor(model => model.PartyPersonalDetail.Gender, new SelectList(Model.Gender, "Id", "Name"), new { @style = "width: 100px;" })
        </td>
        <td>@Html.TextBoxFor(m => m.PartyPersonalDetail.MobilePhone, new { @style = "width: 100px;" })
        </td>
        <td>
            @Html.DropDownListFor(model => model.PartyPersonalDetail.GothraId, new SelectList(Model.Gothras, "Id", "GothraName"), "--Don't Know--", new { @style = "width: 122px;" })
        </td>
        <td>
            @Html.DropDownListFor(model => model.PartyPersonalDetail.NakshtraId, new SelectList(Model.Nakshtras, "Id", "NakshtraName"), "--Don't Know--", new { @style = "width: 122px;" })
        </td>
        <td>@Html.TextBoxFor(m => m.PartyPersonalDetail.EMail1, new { @style = "width: 135px;" })
        </td>
        <td>
            <a href="#" class="deleteRow">Delete</a>
        </td>

    }
</tr>

@使用(Html.BeginCollectionItem(“参与方”))
{
@Html.TextBoxFor(m=>m.FirstName,新的{@style=“width:100px;”})
@Html.TextBoxFor(m=>m.LastName,新的{@style=“width:100px;”})
@DropDownListFor(model=>model.partypersonaldeail.Gender,新选择列表(model.Gender,“Id”,“Name”),新{@style=“width:100px;”)
@Html.TextBoxFor(m=>m.PartyPersonalDetail.MobilePhone,新的{@style=“width:100px;”})
@Html.DropDownListFor(model=>model.partypersonaldeail.GothraId,新选择列表(model.Gothras,“Id”,“GothraName”),“--不知道--”,新{@style=“width:122px;”
@Html.DropDownListFor(model=>model.partypersonaldeail.nakstraid,新选择列表(model.Nakshtras,“Id”,“NakshtraName”),“--不知道--”,新{@style=“width:122px;”
@Html.TextBoxFor(m=>m.PartyPersonalDetail.EMail1,新的{@style=“width:135px;”)
}

为了在MVC中发布集合,它们必须被索引,因此需要一个
for
循环,而不是
foreach

试试这个:

@for(int i = 0; i < Model.Participants.Count; i++)
{
    Html.RenderPartial("BlankEditorRow", Model.Participants[i]);
}
@for(int i=0;i
使用编辑器模板

假设您的Viewmodel是这样的

public EventViewModel
{
  public int ID { set;get;}
  public string EventName { set;get;}
  public List<Participant> Participants {set;get;}
  public EventViewModel()
  {
    Participants=new List<Participant>();
  }
}

public class Participant
{
  public string FirstName { set;get;}
  public string LastName { set;get;}
}
现在在主视图中,使用
Html.EditorFor
Html助手方法调用此编辑器模板

@model EventViewModel

@using (Html.BeginForm())
{
    <p>Event Name</p>
    @Html.TextBoxFor(x => x.EventName) 
    @Html.EditorFor(x=>x.Participants)
    <input type="submit" value="Save" />
}
@model EventViewModel
@使用(Html.BeginForm())
{
事件名称

@Html.TextBoxFor(x=>x.EventName) @EditorFor(x=>x.Participants) }

现在,当您发布表单时,您将在viewmodel的参与者集合中获得参与者信息。

partialModel从视图中的何处来?这是模型的属性吗?@scartag:请查看编辑的问题“Model.Participants.Length”给出了一个错误,因为没有可用的“.Length”方法。我使用了“Model.Participants.Count”,在httpPost中仍然得到空值method@Shreesh对不起,我是说伯爵。您可以发布您的部分视图吗?@shresh使用(Html.BeginCollectionItem(“party”))包装在部分视图周围,删除
@
@model EventViewModel

@using (Html.BeginForm())
{
    <p>Event Name</p>
    @Html.TextBoxFor(x => x.EventName) 
    @Html.EditorFor(x=>x.Participants)
    <input type="submit" value="Save" />
}