2016-12-27 95 views
-3

我的模式是:制作用户在ASP.NET MVC中选择一个单选按钮

,并在视图中,我使用:

 <td style="vertical-align:top" Width="60%">Is this OK ?</td> 
     <td> 
      @Html.RadioButton("OK", "Yes") Yes @Html.RadioButton("Ok", "No") No 

     </td> 

我需要@Html.RadioButtonFor还是我失去了还有一些因为单选按钮验证未激活。

提交按钮,我只是在做

<button type="submit" class="btn btn-primary">Sign up</button> 

和BeginForm是: -

@using(Html.BeginForm("addsurvey","survey")) 
{ 

回答

0

应指定属性绑架和使用的名称:

<td style="vertical-align:top" Width="60%">Is this OK ?</td> 
<td> 
    @Html.RadioButtonFor(m => m.Abduction, "Yes") @:Yes 
    @Html.RadioButtonFor(m => m.Abduction, "No") @:No 
</td> 

<td style="vertical-align:top" Width="60%">Is this OK ?</td> 
<td> 
    @Html.RadioButton("Abduction ", "Yes") Yes 
    @Html.RadioButton("Abduction ", "No") No 
</td> 
+0

验证仍不起作用。 – MiscellaneousUser

+0

你是什么意思?不显示“你必须选择一个OK选项”或其他东西?请解释什么是问题,因为很难准确地分辨出你写的“不起作用”时所要求的。 – Marusyk

+0

如果我单击提交按钮(添加),则提交页面时无需选择单选按钮。验证不起作用,并且绑定的值为空而不是“是”或否。 – MiscellaneousUser

0

该页面将提交,因为没有任何内容阻止它提交。然后,您可以检查ModelState.IsValid,看是否提交什么可用/允许/有效的,这样的:

public ActionResult addsurvey(Survey srv) 
{ 
    if (ModelState.IsValid) 
    { 
     // OK: Save, Proceed and/or Redirect 
    } 
    else 
    { 
     // not OK: show View again 
     return View(srv); // or: View("ViewName", srv); 
    } 
} 

在视图中,请务必使用@Html.ValidationSummary()这将显示用户为什么ModelState中无效。

+0

当我返回View(srv)时,它会转到/ addsurvey而不是刷新页面。 – MiscellaneousUser

+0

请参阅我的编辑。 –

+0

我有重新加载,但没有验证消息出现,我选择的选项之前不再选择。 – MiscellaneousUser