2011-05-06 50 views
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; } 
} 

回答

0

的問題是,在一側您正在使用html屬性檢查無線電的值,另一方面,您希望您的模型值設置相同的v ALUE。所以你想要做的事情可以通過簡單地從您的視圖中刪除@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到 公共類客戶端用戶

{ 
    public ClientDetail ClientDetails { get; set; }  
    public List<Phone> Phones { get; set; } 
} 
+0

太棒了!你的回答做到了訣竅並且合理。 關於ClientDetails,這段代碼只是我所擁有的簡單版本。在我正在開發的應用程序中,用戶具有不同的ClientDetails集。默認情況下,當用戶提交信息時,它將保存第一組ClientDetails,但他可以有更多。 – SaltProgrammer 2011-05-06 16:33:25