在下拉列表更改事件上加载部分表单

时间:2012-07-04 19:47:00

标签: jquery asp.net asp.net-mvc asp.net-mvc-3

我有一个牌照下拉列表,其中最后一项是"新牌照"。默认情况下,表单不会显示新的车牌表单字段(新车牌的文本框,以及状态/省份来源的下拉列表)。

如何根据"新牌照"

的选择显示表格?

到目前为止我创建了一个局部视图:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Namespace.ViewModels.ProfileMemberViewModel>" %>
<table cellspacing="2" cellpadding="2" width="100%" border="0" align="left">
<tr>
    <td>
        <%=Html.LabelFor(model => model.NewLicensePlate.LicensePlate, new { @class = "inputLabel" })%>
    </td>
    <td>
        <%=Html.TextBoxFor(model => model.NewLicensePlate.LicensePlate, new { @class = "inputField" })%>
    </td>
</tr>
<tr>
    <td>
        <%=Html.LabelFor(model => model.NewLicensePlate.PlateState, new { @class = "inputLabel" })%>
    </td>
    <td>
        <%=Html.DropDownListFor(model => model.NewLicensePlate.PlateState, Model.NewLicensePlate.PlateStates, new { @class = "inputField" })%>
    </td>
</tr>

父母表格:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Paysite.Providers.ViewModels.ProfileMemberViewModel>" %>
<% using (Html.BeginForm("Confirm", "Profile", FormMethod.Post, new { @id = "Profile-form" }))
   {%>
...
<tr>
    <td>
        <%=Html.LabelFor(model => model.Member.LicensePlate, new { @class = "inputLabel" })%>
        <br />
        <br />
        <span class="small">(To add, select "New License Plate")</span>
    </td>
    <td>
        <%=Html.DropDownListFor(model => model.Member.LicensePlate, Model.Member.LicensePlates, new { @class = "inputField" })%>
    </td>
</tr>
</table>
<div id="NewLicensePlate">            
</div>
...
<%
   } %>

父母形式的jQuery:

$("#Member_LicensePlate").change(function () {
  var selectedPlate = $("#Member_LicensePlate").val();
  if (selectedPlate == "New License Plate") {
    $("#NewLicensePlate").load('<%= Url.Action("NewLicensePlate") %>');
  } else {
    $('#NewLicensePlate').empty();
  }
});

的ViewModels:

public class ProfileMemberViewModel
{
    public ExistingMemberViewModel Member { get; set; }
    public NewLicensePlateViewModel NewLicensePlate { get; set; }
}

public class NewLicensePlateViewModel
{
    [Display(Name = "License Plate")]
    public string LicensePlate { get; set; }

    [Display(Name = "Province/State")]
    public string PlateState { get; set; }

    public IEnumerable<SelectListItem> PlateStates { get; set; }
}

控制器操作:

public ActionResult NewLicensePlate()
{
    var model = new ProfileMemberViewModel();
    model.NewLicensePlate = new NewLicensePlateViewModel();

    DataSet statesDataSet = _statesRepo.GetStates();

    model.NewLicensePlate.PlateStates =
            Enumerable.Select(statesDataSet.Tables[0].AsEnumerable(), x => new SelectListItem
        {
            Text = x.Field<string>("NAME"),
            Value = x.Field<double>("ID").ToString()
        });

    return PartialView("NewLicensePlate", model);
}

当我从父母发布到public ActionResult Confirm(ProfileMemberViewModel model)时,model.NewLicensePlate无法获得绑定,而是null。我在这里做错了吗?我不确定这是否是最好的方法。

1 个答案:

答案 0 :(得分:1)

我认为最简单的方法是将所有字段放在相同的(父窗体)中。隐藏新板的控件。 使用javascript,您可以根据“新车牌”的选择显示它们。 发布表单时,如果控件已填写,则处理新的牌照