我在页面上有两组单选按钮:
Phone 1: [ ] Home | [x] Work | [ ] Cell
Phone 2: [ ] Home | [ ] Work | [x] Cell
当您看到该页面时,我正在设置Phone 1,“Work”和Phone 2,“Cell”的默认值。发生的事情是,当用户提交并且未输入所需的FirstName时, AND 选择Home(对于Phone 1)和Home(Phone 2) - 当页面刷新时,Phone 1为“Home”和Phone 2是“Cell”。
这是怎么回事?电话2应该是“主页”,因为这是我在收到错误消息之前选择的内容。非常感谢任何反馈!
以下是观点:
@using (Html.BeginForm("create", "PetSittingRequest"))
{
<label class="labelleft" style="width: 100px">First Name:</label>
<div class="formfield" style="width: 205px">
@Html.TextBoxFor(model => model.ClientDetails[0].NameFirst, new { style = "width: 195px" })
@Html.ValidationMessageFor(model => model.ClientDetails[0].NameFirst, null )
</div>
// Phone #1 START
<label class="labelleft" style="width: 100px">Phone 1:</label>
<div class="formfield" style="width: 60px">
@Html.TextBoxFor(model => model.Phones[0].PhoneNumber, new { style = "width: 195px" })
@Html.ValidationMessageFor(model => model.Phones[0].PhoneNumber, null)
</div>
<div class="formfield" style="width: 60px"></div>
<div class="formfield" style="width: 95px"></div>
<div class="formfield" style="width: 60px">
@Html.RadioButtonFor(model => model.Phones[0].Location, "Home", new { @class="radiobtn" } ) Home
</div>
<div class="formfield" style="width: 60px">
@Html.RadioButtonFor(model => model.Phones[0].Location, "Work", new { @class="radiobtn", @checked = true } ) Work
</div>
<div class="formfield" style="width: 60px">
@Html.RadioButtonFor(model => model.Phones[0].Location, "Cell", new { @class="radiobtn" } ) Cell
</div>
// Phone #2 START
<label class="labelleft" style="width: 100px">Phone 2:</label>
<div class="formfield" style="width: 60px">
@Html.TextBoxFor(model => model.Phones[1].PhoneNumber, new { style = "width: 195px" })
@Html.ValidationMessageFor(model => model.Phones[1].PhoneNumber, null)
</div>
<div class="formfield" style="width: 60px"></div>
<div class="formfield" style="width: 95px"></div>
<div class="formfield" style="width: 60px">
@Html.RadioButtonFor(model => model.Phones[1].Location, "Home", new { @class="radiobtn" } ) Home
</div>
<div class="formfield" style="width: 60px">
@Html.RadioButtonFor(model => model.Phones[1].Location, "Work", new { @class="radiobtn" } ) Work
</div>
<div class="formfield" style="width: 60px">
@Html.RadioButtonFor(model => model.Phones[1].Location, "Cell", new { @class="radiobtn", @checked = true } ) Cell
</div>
}
处理帖子的控制器是:
[HttpPost]
public ActionResult Create(ClientUser x)
{
try
{
return View("Index", x);
}
catch
{
return View();
}
}
以下是模型:
public class ClientUser
{
public List<ClientDetail> ClientDetails { get; set; }
public List<Phone> Phones { get; set; }
}
public class ClientDetail
{
[Required(ErrorMessage="This field is required.")]
public string NameFirst { get; set; }
}
public class Phone
{
[Required(ErrorMessage="This field is required.")]
public string PhoneNumber { get; set; }
[Required(ErrorMessage="This field is required.")]
public string Location { get; set; }
}
答案 0 :(得分:0)
问题在于,您在一侧使用html属性检查无线电的值,另一方面,您希望模型值设置相同的值。所以你想要做的只需从每个单选按钮的视图中删除@checked = true,如果你需要在第一次表单渲染时选择一些特定的值,你可以做类似的事情
public ActionResult Create()
{
ClientUser clientUser = new ClientUser();
clientUser.ClientDetails = new List<ClientDetail> { new ClientDetail()};
clientUser.Phones = new List<Phone> { new Phone{Location = "Work"}, new Phone{Location = "Cell"} };
return View(clientUser);
}
这会将第一个无线电组的值设置为“工作”,将第二个组设置为“单元格”,并且当您在表单上进行更改时也会相应地更改值,并在出现错误时将其呈现。此外,当您在表单上只显示一个用户时,为什么要有clientDetails列表。我建议将viewmodel更改为 公共类ClientUser
{
public ClientDetail ClientDetails { get; set; }
public List<Phone> Phones { get; set; }
}