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