【问题标题】:How to post data from partial view in ASP.NET MVC3?如何在 ASP.NET MVC3 中从部分视图发布数据?
【发布时间】:2013-01-06 23:58:37
【问题描述】:

我有一个使用视图模型的视图。 在这个视图中,我在@using (Html.BeginForm()) 块代码中使用了部分视图,如下所示

 @foreach (var party in Model.Participants)
                {
                    Html.RenderPartial("BlankEditorRow", party);
                }

部分视图有一些文本框字段,用户可以在这些字段中输入数据。 现在提交按钮不会放在局部视图中,而是放在主视图中。

在我看来,当我单击提交按钮时,我在视图模型中得到空值

[HttpPost]
    public ActionResult ActionName(ViewModel model)
    {

    }

我不确定如何从部分视图中获取帖子数据。谁能帮我理解如何从部分视图发布数据?示例将是一个很大的帮助

编辑:部分视图如下:

@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>

【问题讨论】:

  • partialModel 是从哪里来的?它是模型的属性吗?
  • @scartag : 请查看已编辑的问题

标签: c# asp.net-mvc asp.net-mvc-3 razor partial-views


【解决方案1】:

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

试试这个:

@for(int i = 0; i < Model.Participants.Count; i++)
{
    Html.RenderPartial("BlankEditorRow", Model.Participants[i]);
}

【讨论】:

  • "Model.Participants.Length" 给出错误,因为没有可用的“.Length”方法。我使用了“Model.Participants.Count”,在 httpPost 方法中仍然得到空值
  • @Shreesh 道歉,我的意思是计数。你能发表你的部分观点吗?
  • @Shreesh 移除包裹在部分周围的@using (Html.BeginCollectionItem("party"))
【解决方案2】:

使用编辑器模板

假设你的 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;}
}

在 ~/Views/yourControllerNameFolder/ 下创建一个名为“EditorTemplates”的文件夹,并创建一个名为 Participant.cshtml 的视图(编辑器模板)。

现在为您的编辑器模板添加代码

@model Participant
<p>
  @Html.TextBoxFor(x=>x.FirstName)
</p>
<p>
  @Html.TextBoxFor(x=>x.LastName)
</p>

现在在您的主视图中,使用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" />
}

现在,当您发布表单时,您将在视图模型的 Participants 集合中获得 Participant 信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 2021-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-10
    • 1970-01-01
    相关资源
    最近更新 更多