在MVC3中使用JQuery在Clinetside进行验证

在MVC3中使用JQuery在Clinetside进行验证,jquery,asp.net-mvc-3,Jquery,Asp.net Mvc 3,这些是我的模型 public class Question { private string _questionNo; private string _questionText; private List<Option> _options; public List<Option> Options { get { return _options; } set { _options = value; } } public string Questio

这些是我的模型

public class Question
{

private string _questionNo;  
private string _questionText; 
private List<Option> _options;

public List<Option> Options
{
    get { return _options; }
    set { _options = value; }
}
public string QuestionNo
{
    get { return _questionNo; }
    set { _questionNo = value; }
} 
public string QuestionText
{
    get { return _questionText; }
    set { _questionText = value; }
} 
这是我的控制器

   public ActionResult GetQuestion(int id,string gid)
    { 
        var vewmodel = ques.GetQuestion(Gid, Uid, id);
        return View(vewmodel);
    }
这是我的看法

     @model mobilesurveys.mt.Models.Question  
     @{ ViewBag.Title = "Question"; }
    <div data-role="header" data-theme="b">
     <h1>
    @Model.SurveyName</h1>
    </div>
   @if (@Model.QuestionType == 7)
   {   
<div data-role="fieldcontain">
    @using (Html.BeginForm("SaveDropDown", "GetQuestion", Model))
    {
        @Html.AntiForgeryToken()

        <fieldset>
            <label class="select">@Model.QuestionText
            </label>
            <br />
            <select name="selectedObjects" id="selectchoice1" data-native-menu="false">
                <option value="--select--">--Select--</option> 
                @foreach (var item in Model.Options)
                {

                    if (@item.IsAnswer == true)
                    {
                    <option selected="selected"  value="@item.OptionNumber">@item.OptionText</option>
                    }
                    else
                    {
                    <option  value="@item.OptionNumber">@item.OptionText</option>
                    }

                }
            </select>
        </fieldset>
        <p>
            <input type="submit" value="Next" />
        </p>
    }
    </div>
   }
@model mobilesurveys.mt.Models.Question
@{ViewBag.Title=“问题”;}
@Model.SurveyName
@如果(@Model.QuestionType==7)
{   
@使用(Html.BeginForm(“SaveDropDown”,“GetQuestion”,Model))
{
@Html.AntiForgeryToken()
@范文

--选择-- @foreach(Model.Options中的var项) { 如果(@item.IsAnswer==true) { @item.OptionText } 其他的 { @item.OptionText } }

} }
因此,我基于QuestionTypeId(RadioButtions、复选框、文本框、Select)绑定optoins。现在我想在客户端验证数据。我正在使用Jquery脚本

如何编写验证。感谢您的帮助

谢谢。

有两种方法。 1.可以通过在模型类中添加注释来完成此操作 在模型中添加类似于此的行

[Required(ErrorMessage = "Title is required"), StringLength(230)]
通过这种方式,在需要的变量上方添加注释

或 2.您可以使用jquery代码进行验证。 我给你密码

$(document).ready(function () {
        $("#SubmitForm").click(function (e) {
            var textContent = $("#TextContent").val();
            textContent = jQuery.trim(textContent);
            if (textContent == "") {
                alert("Content field cannot be empty.");
                $("#TextContent").focus();
                return false;
            }


        });
    });
通过这种方式,您可以检查jquery中的字段。
如果您发现任何问题,请回答我

看看这个,我已经看到了这些例子。但我不能在模型上定义验证。它每次都会改变。第一次使用select,下一次使用checkboxlist,下一次使用一些随机类型。我有一个有问题的模型列表。我们有没有其他方法在IList上定义验证。你可以使用jquery方法验证你的控件chk it,你会发现它很有用,如果你有任何问题,请回答我。加载第二个视图时,它不会触发事件。实际上,我正在重定向到具有不同参数的同一视图。脚本仅在第一个视图中完美执行。任何帮助。我把它作为一个新问题发布在
$(document).ready(function () {
        $("#SubmitForm").click(function (e) {
            var textContent = $("#TextContent").val();
            textContent = jQuery.trim(textContent);
            if (textContent == "") {
                alert("Content field cannot be empty.");
                $("#TextContent").focus();
                return false;
            }


        });
    });