我正在使用 asp.net mvc 4 有没有办法根据用户的选择来更新表单?
(在这种情况下,如果从下拉列表中选择了某些内容,我想填写地址字段,否则需要输入新地址)
我的模型:公共类 NewCompanyModel {
[Required]
public string CompanyName { get; set; }
public bool IsSameDayRequired { get; set; }
public int AddressID { get; set; }
public Address RegisterOfficeAddress { get; set; }
}
看法:
@model ViewModels.NewCompanyModel
@using (Html.BeginForm(null, null, FormMethod.Post, new { name = "frm", id = "frm" }))
{
@Html.ValidationSummary(true)
<fieldset id="test">
<legend>Company</legend>
<h2>Register office address</h2>
<div class="editor-label">
@Html.LabelFor(model => model.AddressID)
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.AddressID, (IList<SelectListItem>)ViewBag.Addresses, new {id = "address", onchange = "window.location.href='/wizard/Address?value=' + this.value;" })
</div>
<div class="editor-label">
@Html.LabelFor(model => model.RegisterOfficeAddress.BuildingNameOrNumber)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.RegisterOfficeAddress.BuildingNameOrNumber)
@Html.ValidationMessageFor(model => model.RegisterOfficeAddress.BuildingNameOrNumber)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.RegisterOfficeAddress.StreetName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.RegisterOfficeAddress.StreetName)
@Html.ValidationMessageFor(model => model.RegisterOfficeAddress.StreetName)
</div>
和控制器:
public ActionResult Address(string value)
{
//get the address from db and somehow update the view
}
问题是你如何更新'model.RegisterOfficeAddress.StreetName'等只是为了明确这只是表格的一部分,所以我还不能提交它。
非常感谢