mvc中的数据验证 [英] data validation in mvc

查看:83
本文介绍了mvc中的数据验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hiiiiiii

i是mvc的新手我通过模型class1尝试了数据验证。

hiiiiiii
i am new to mvc i tried datavalidations by taking model class1.

public class Class1
   {
       [Required(ErrorMessage = "name is required")]
       public string name { set; get; }
       [Required(ErrorMessage = "id is required")]
       public string id{set;get;}
       [Required(ErrorMessage = "gender is required")]
       public string gender{set;get;}
       [Required(ErrorMessage = "hobbies is required")]
       public string hobbies { set; get; }

   }





i取得控制器并添加了一个视图,其中包含create scafflonding.i在视图中添加了脚本。 br />



i taken controller and added a view with create scafflonding.i added scripts in view.

<head runat="server">
 <script src="../../Scripts/jquery-1.4.1.js" type="text/javascript"></script>
    <script src="../../Scripts/jquery.validate.js" type="text/javascript"></script>
    <script src="../../Scripts/MicrosoftAjax.js" type="text/javascript"></script>
    <script src="../../Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
    <script src="../../Scripts/MicrosoftMvcValidation.js" type="text/javascript"></script>
  <% Html.EnableClientValidation();%>
    <title>Index</title>
</head





i我没有得到任何验证错误。任何人都可以帮助我。

视图是默认生成的。



i am not getting any validation error.can any one help me please.
the view is default generated.

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<MvcApplication1.Models.Class1>" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
 <script src="../../Scripts/jquery-1.4.1.js" type="text/javascript"></script>
    <script src="../../Scripts/jquery.validate.js" type="text/javascript"></script>
    <script src="../../Scripts/MicrosoftAjax.js" type="text/javascript"></script>
    <script src="../../Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
    <script src="../../Scripts/MicrosoftMvcValidation.js" type="text/javascript"></script>
  <% Html.EnableClientValidation();%>
    <title>Index</title>
</head>
<body>
     <% using (Html.BeginForm()) {%>
       <%-- <%: Html.ValidationSummary(true," unsuccessfull pleses fill correctly") %>--%>

        <fieldset>
            <legend>Fields</legend>

            <div class="editor-label">
                <%: Html.LabelFor(model => model.name) %>
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.name) %>
                <%: Html.ValidationMessageFor(model => model.name) %>
            </div>

            <div class="editor-label">
                <%: Html.LabelFor(model => model.id) %>
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.id) %>
                <%: Html.ValidationMessageFor(model => model.id) %>
            </div>

            <div class="editor-label">
                <%: Html.LabelFor(model => model.gender) %>
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.gender) %>
                <%: Html.ValidationMessageFor(model => model.gender)%>
            </div>

            <div class="editor-label">
                <%: Html.LabelFor(model => model.hobbies) %>
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.hobbies) %>
                <%: Html.ValidationMessageFor(model => model.hobbies)%>
            </div>

            <p>
                <input type="submit" value="Create" />
            </p>
        </fieldset>

    <% } %>

    <div>
        <%: Html.ActionLink("Back to List", "Index") %>
    </div>

</body>
</html>

推荐答案

嗨Lakshmi,



看到你的代码后,一切似乎都很完美,除了控制器。因为你错过了在这里发布。



In你的行为是否有像这样的modelstate.isValid

Hi Lakshmi,

After seeing your code everything seems to be perfect, except controller.Since you missed to post it here.

In your action do you have modelstate.isValid like this
public ActionResult actionName(class1 clsObj)
{
  if(ModelState.IsValid)
   {
   // TODO: Add your own logic here
   }
  else
   {
    return View(clsObj);
   }
}



评论后更新的解决方案,


Updated solution after your comments,

Hey, in ASP.NET MVC3, there's no need to add Html.EnableClientValidation() in view page, instead, just enable the clientValidation in the webconfig file as below:
<appSettings>
  <add key="ClientValidationEnabled" value="true" />
  <add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>



希望这对您有所帮助。



问候,

RK


Hope this helps you a bit.

Regards,
RK


您好



add课程为

@class =required



Ex: - <%= Html.TextBoxFor(model => model.LastName,new {@class =required})%>



试试希望这会有所帮助
Hi

add class as
@class = "required"

Ex:- <%= Html.TextBoxFor(model => model.LastName, new { @class = "required" })%>

Try this hope this will help


这篇关于mvc中的数据验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆