2
我有两个组单选按钮的页面上:单选按钮不坚持在提交时,有一个错误
Phone 1: [ ] Home | [x] Work | [ ] Cell
Phone 2: [ ] Home | [ ] Work | [x] Cell
当您看到的页面,我设置的默认设置电话1,“工作”,和Phone 2“Cell”。发生的事情是,当用户提交并且没有输入所需的姓氏时,和选择家庭(电话1)和家庭(电话2) - 当页面刷新时,电话1是“家庭”,电话2是“细胞”。
这是怎么回事?电话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; }
}
太棒了!你的回答做到了诀窍并且合理。 关于ClientDetails,这段代码只是我所拥有的简单版本。在我正在开发的应用程序中,用户具有不同的ClientDetails集。默认情况下,当用户提交信息时,它将保存第一组ClientDetails,但他可以有更多。 – SaltProgrammer 2011-05-06 16:33:25