2011-05-06 53 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