ASP.NET MVC中页面加载时显示验证错误 [英] Validation error displayed on page load in ASP.NET MVC

查看:75
本文介绍了ASP.NET MVC中页面加载时显示验证错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 

当我点击用于在asp.net mvc中添加新帖子表格的URL时。在页面加载时,将显示验证错误。



错误是红色的,实际上应该在提交按钮上提示。



用户名是需要

标题是必需的

描述是必需的



我尝试了什么:



添加帖子控制器



  public  ActionResult AddPost(ForumPost post)
{
AdminObserver observer1 = new AdminObserver();

ActivityObserver observer2 = new ActivityObserver();

post.PostedOn = DateTime.Now;

if (!ModelState.IsValid)
{
return 查看(发布);

}

使用(AppDbContext db = new AppDbContext())
{
try
{
db.ForumPosts.Add(post);
db.SaveChanges();
}
catch (DbEntityValidationException e)
{

Console.WriteLine(e);
}
}
TempData [ SM] = < span class =code-string> 您添加了一条新记录!;
ForumNotifier notifier = new ForumNotifier();

// ForumNotifier notifier = new ForumNotifier();


notifier.Subscribe(observer1);

notifier.Subscribe(observer2);

notifier.Notify(post);

return 查看( AddPost,发布);
}





添加帖子表格

 <   h2  >  AddPost <   / h2  >  
@if( TempData [SM]!= null)
{
< div class = alert alert-success >
@TempData [SM]
< / div >
}

@using(Html.BeginForm(AddPost,Home))
{
@ Html.AntiForgeryToken()

< div class = form-horizo​​ntal >
< h4 > ForumPost < / h4 >
< hr / >
@ Html.ValidationSummary(true,,new {@class =text-danger})
< div class = 表单组 >
@ Html.LabelFor(model => model.UserName,htmlAttributes:new {@class =control-label col-md-2})
< div class = col-md-10 >
@ Html.EditorFor(model = > model.UserName,new {htmlAttributes = new {@class =form-control}})
@ Html.ValidationMessageFor(model => model.UserName,,new {@class =text -danger})
< / div >
< / div >

< div class = form- group >
@ Html.LabelFor(model => model.Title,htmlAttributes:new {@class =control-label col-md-2})
< div class = col-md-10 >
@ Html.EditorFor(model = > model.Title,new {htmlAttributes = new {@class =form-control}})
@ Html.ValidationMessageFor(model => model.Title,,new {@class =text -danger})
< / div >
< / div >

< div class = form-group >
@ Html.LabelFor(model => model.Description,htmlAttributes:new {@class =control-label col-md-2})
< div class = col-md-10 >
@ Html.EditorFor(model = > model.Description,new {htmlAttributes = new {@class =form-control}})
@ Html.ValidationMessageFor(model => model.Description,,new {@class =text -danger})
< / div >
< / div >

< h4 > @ ViewBag.Message < / h4 >

< div class = form-group >
< div class = col-md-offset-2 col-md-10 >
< 输入 类型 = submit value = 创建 class = btn btn-default / >
< / div >
< / div >
< ; / div >
}

解决方案

我认为该方法应该有一个post属性。不确定你的代码是如何构建的,应该有一个get和post的动作



  //   GET:AddPost  
public ActionResult AddPost()
{
return 查看();
}

// POST:AddPost
[HttpPost]
public ActionResult AddPost(ForumPost post)
{
....
}


When I click on the URL for add new post form in asp.net mvc. On page load the validations errors are displayed.

The errors are in red and should actually be prompted on submit button.

The user name is required
The title is required
Description is required

What I have tried:

Add Post Controller

public ActionResult AddPost(ForumPost post)
        {
            AdminObserver observer1 = new AdminObserver();

            ActivityObserver observer2 = new ActivityObserver();

            post.PostedOn = DateTime.Now;

            if (!ModelState.IsValid)
            {
                return View(post);
               
            }
          
                using (AppDbContext db = new AppDbContext())
                {
                    try
                    {
                        db.ForumPosts.Add(post);
                        db.SaveChanges();
                    }
                    catch (DbEntityValidationException e)
                    {

                        Console.WriteLine(e);
                    }
                }
                TempData["SM"] = "You have added a new Record!";
                ForumNotifier notifier = new ForumNotifier();

                //ForumNotifier notifier = new ForumNotifier();


                notifier.Subscribe(observer1);

                notifier.Subscribe(observer2);

                notifier.Notify(post);
           
            return View("AddPost", post);
        }



Add Post form

<h2>AddPost</h2>
@if (TempData["SM"] != null)
{
    <div class="alert alert-success">
        @TempData["SM"]
    </div>
}

@using (Html.BeginForm("AddPost","Home")) 
{
    @Html.AntiForgeryToken()
    
    <div class="form-horizontal">
        <h4>ForumPost</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.UserName, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.UserName, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.UserName, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
            </div>
        </div>

        <h4>@ViewBag.Message</h4> 

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

解决方案

I think that method should have a post attribute. Not sure how your code is being structure, there should be a get and post for the action

// GET: AddPost
        public ActionResult AddPost()
        {
            return View();
        }

  // POST: AddPost
 [HttpPost]
        public ActionResult AddPost(ForumPost post)
        {
            ....
        }


这篇关于ASP.NET MVC中页面加载时显示验证错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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