c# - 如何验证 MVC 上的 RadioButton?

标签 c# .net asp.net asp.net-mvc

我正在使用客户端验证,考虑到我正在制作表格,它开始变得困惑。对于所有的文本框和单选按钮验证, Controller 将不堪重负。如何在 MODEL 端验证和显示 MVC 中单选按钮和多个文本框的错误消息?

我所拥有的简化版本。

型号...

public class ModelData
{
    public string ContactName { get; set; }
    public string ContactAddress { get; set; }
    public string ContactPhone { get; set; }
    public bool RadioPoliceFire { get; set; }
    public bool RadioComplaint { get; set; }

    //The following is a Failure :(
    public string RadioType
    {
        if (RadioType == null)
            {return "Type Required";}
        return null;
    }
    //End Failure
}

Controller ...

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Info(ModelData InfoData)
{
    if (infoData.RadioType == null)
        {ModelState.AddModelError("RadioType", "Type Required");}
    try
    { ...
         return RedirectToAction("Confirmation");
    catch
    {ModelState.AddModelError("RadioComplaint", "Error");}
}

最佳答案

我喜欢 DataAnnotations 验证 http://www.asp.net/learn/mvc/tutorial-39-cs.aspx或MVC 2 http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx

顺便说一句,IsRadioButtonChecked 应该是 bool 值。

关于c# - 如何验证 MVC 上的 RadioButton?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2593335/

相关文章:

c# - WPF 中的横向-纵向方向

c# - C# 应用程序中的自定义用户配置

c# - 显示名称为 'System.Windows.Interactivity'的程序集无法加载

c# - 指定的转换在 linq 查询中无效

javascript - ASP.net javascript 在 asp 上起火 :ImageButton

javascript - Asp.Net MVC 重写 CSS 类

c# - 如何使 UserControls 的代码隐藏继承基类?

c# - 为位于 PageFooter 部分的 TableObject 执行脚本时出错

asp.net - 尽管 asp.net mvc 4 验证失败,jquery post 仍然会通过

c# - 维基百科 A* 寻路算法需要大量时间