如何在MVC中向客户端和服务器端的razor页面添加验证。 [英] How to add validation to razor page in MVC both client side and server side.

查看:103
本文介绍了如何在MVC中向客户端和服务器端的razor页面添加验证。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是MVC的新手并尝试构建代码,但得到错误为

I'm New To MVC and Trying to build the code but get's error as "

Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.







我的型号是: -




"

My Model is:-

namespace RsbEngMvc.Models
{
    [Table("TblWhyUs")]
    public class WhyUs
    {
        public int ID { get; set; }

        [Required(ErrorMessage="Please Enter Heading")]
        [StringLength(100, ErrorMessage = "Heading cannot be longer than 100 characters.")]
        public string Heading { get; set; }
        
        [Display(Name = "Body Text")]
        [Required(ErrorMessage = "Please Enter Body Text")]
        [StringLength(2000, ErrorMessage = "Body Text cannot be longer than 2000 characters.")]
        public string Body { get; set; }
        
        [Display (Name="Image")]
        [StringLength(150, ErrorMessage = "Image Name cannot be longer than 150 characters.")]
        public string Img { get; set; }
    }
}





和控制器是: -



and Controller Is:-

[HttpPost]
        [ActionName("AdminWhyUsCreate")]
        public ActionResult AdminWhyUsCreate_Post(FormCollection formCollection,HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {
              

                WhyUs whyuss = new WhyUs();
                //string Heading = formCollection["Heading"];
                //string Body = formCollection["Body"];
                //string Img = Convert.ToString(formCollection["Img"]);
                //string PageName = "WhyUsCreate";

                //Create(Heading, Body, PageName);
                
                if (file != null)
                {
                    file.SaveAs(HttpContext.Server.MapPath("~/Images/") + file.FileName);
                    whyuss.Img = file.FileName;
                }
                //else
                //{
                //    whyuss.Img = "";
                //}
                whyuss.Heading = formCollection["Heading"];
                whyuss.Body = formCollection["Body"];

                RsbEnggContext DbContext = new RsbEnggContext();
                DbContext.whyus.Add(whyuss);
                DbContext.SaveChanges();

                return RedirectToAction("AdminWhyUs");
            }

            return View();
        }





我的尝试:



我在我的视图中尝试了Jquery验证但是它不起作用: -

@using(Html.BeginForm(AdminWhyUsCreate,MyAdmin,FormMethod.Post,new {enctype =multipart / form-data}))

{



@ Html.AntiForgeryToken()

@ Html.ValidationSummary(true)





在WhyUs中创建新条目





What I have tried:

I tried Jquery validation in my View Also but it's not working :-
@using (Html.BeginForm("AdminWhyUsCreate", "MyAdmin", FormMethod.Post, new { enctype = "multipart/form-data" }))
{

@Html.AntiForgeryToken()
@Html.ValidationSummary(true)


Create New Entry in WhyUs



@ Html.LabelFor(model => model.Heading)


@Html.LabelFor(model => model.Heading)






@ Html.TextBoxFor(model => model.Heading,new {@class =form-control,maxlength =100 })

@ Html.ValidationMessageFor(model => model.Heading)


@Html.TextBoxFor(model => model.Heading, new { @class = "form-control",maxlength="100" })
@Html.ValidationMessageFor(model => model.Heading)









@ Html.LabelFor(model => model.Body)


@Html.LabelFor(model => model.Body)






@ Html.TextAreaFor(model => ; model.Body,new {@class =form-control,@ from = 5,maxlength =3000})

@ Html.ValidationMessageFor(model => model.Body)


@Html.TextAreaFor(model => model.Body, new { @class = "form-control", @rows = 5,maxlength="3000" })
@Html.ValidationMessageFor(model => model.Body)









@ Html.LabelFor( model => model.Img)


@Html.LabelFor(model => model.Img)






@ * @ Html .EditorFor(model => model.Img)* @

@ * @ using(Html.BeginForm(Upload,Upload,FormMethod.Post,new {cssclass =btn btn- info,enctype =multipart / form-data}))* @

@ * {* @

< input type =fileid =FileUpload name =file/>

@ * @ Html.ValidationMessageFor(model => model.Img)* @

@ *} * @


@*@Html.EditorFor(model => model.Img)*@
@*@using (Html.BeginForm("Upload", "Upload", FormMethod.Post, new { cssclass = "btn btn-info", enctype = "multipart/form-data" }))*@
@* { *@
<input type="file" id="FileUpload" name="file" />
@* @Html.ValidationMessageFor(model => model.Img)*@
@* } *@



< input type =submitvalue =Createclass =btn btn-dark/> ;


<input type="submit" value="Create" class="btn btn-dark" />



@ Html.ActionLink(返回列表,AdminWhyUs)


@Html.ActionLink("Back to List", "AdminWhyUs")









< script type =text / javascript>

$(document).ready(function(){

$('#Create')。点击(function(){

if($('#Heading')。val ==){

alert('标题不能为记录空白');

返回false;

}

else if($('#Body ')。val ==){

alert('身体索引不能为记录空白');

返回false;

}

返回true;

});

});



它给出错误: -



一个或多个实体的验证失败。有关详细信息,请参阅'EntityValidationErrors'属性。



此行: -

DbContext.SaveChanges();





<script type="text/javascript">
$(document).ready(function () {
$('#Create').click(function () {
if ($('#Heading').val == "") {
alert('Heading Cannot Be Blank for Record');
return false;
}
else if ($('#Body').val == "") {
alert('Body Index Cannot Be Blank for Record');
return false;
}
return true;
});
});

It Gives the error :-

Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.

On This Line:-
DbContext.SaveChanges();

推荐答案

(document).ready(function(){
(document).ready(function () {


('#Create')。click(function(){

if(
('#Create').click(function () {
if (


('#Heading')。val ==){

alert('标题不能为空记录');

返回false;

}

else if(
('#Heading').val == "") {
alert('Heading Cannot Be Blank for Record');
return false;
}
else if (


这篇关于如何在MVC中向客户端和服务器端的razor页面添加验证。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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