客户端验证不在Asp.net MVC4中工作 [英] Client side validations not Working In Asp.net MVC4

查看:81
本文介绍了客户端验证不在Asp.net MVC4中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是型号命名:AlbumData



Following Is Model named : AlbumData

namespace SanskarUrban.Models
{
    public class AlbumData: List<Image>
    {
        public int Id { get; set; }

        [Required(ErrorMessage = "Album Name Required";)]
        public string AlbumName { get; set; }
     }
}







以下是查看




Following Is View

@model SanskarUrban.Models.Album 
@{
    ViewBag.Title = "AlbumCreation";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<script src="@Url.Content("~/Script/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Script/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

 

@using (Html.BeginForm("AlbumCreation", "AlbumCreation", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
     <div class="wrapper row3">
        <div class="rnd">
            <div id="container" class="clear">
                <div id="gallery" class="clear">
                    
                    <table width="60%">
                        <tr>
                            <td style="width: 25%">
                                Album Name:
                            </td>
<td style="width: 75%">@Html.TextBoxFor(a => a.AlbumName, new { style = "width:250px", placeholder = "AlbumName" } )
@Html.ValidationMessageFor(model => model.AlbumName,null, new { @class = "required" })
                         
                         
                            </td>
                        </tr>
                     
                        <tr>
                             <td>
                               Image Icon:
                            </td>
                            <td>
                            <input type="file" name="file"/>
                            </td> 
                        </tr>
                        <tr>
                            <td>
                            </td>
                            <td>
                             <input type="submit" value="Submit" name="Save" />
                            </td>
                        </tr>
                    </table>
                </div>
            </div>
        </div>
    </div>

}







下一步我的控制器命名为:AlbumCreationController






Next Is My Controller named :AlbumCreationController

using System.Linq;
using System.Reflection;
using System.Web.Mvc;
using SanskarUrban.Models;
namespace SanskarUrban.Controllers
{
    public class AlbumCreationController : Controller
    {

        public ActionResult AlbumCreation(int? Id)
        {

            return View();
        }
    

        [HttpPost]
        [ActionName("AlbumCreation")]
        [AlbumCreationController.AcceptButtonAttribute(ButtonName = "Save")]
        public ActionResult AlbumCreation(Models.Album nd)
        {
            if (ModelState.IsValid)
            {
                ViewBag.Name = nd.AlbumName;
                foreach (string name in Request.Files)
                {
                    var file = Request.Files[name];

                    if (file != null)
                    {
                      string fileName =System.IO.Path.GetFileName(file.FileName);
                      var image = new Image(fileName, Request["description"]);

                        var model = new AlbumData();
                        model.Add(image, file);

                        var context = new Models.SanskarUrbanContainer();
                        var t = new Models.Album
                                    {
                                        AlbumName = nd.AlbumName,
                                        IsActive = true,
                                        Icon = fileName,

                                    };

                        context.Albums.AddObject(t);
                        context.SaveChanges();
                    }
                }
               
            }

            return RedirectToAction("AlbumCreation", "AlbumCreation");
        }
}

推荐答案

此处如果模型状态无效,您将返回相同的状态通过RedirectToAction翻页,而不是你应该返回与模型相同的视图,以便验证消息被绑定。

Here if the model state is not valid, you are returning back to the same page through RedirectToAction, rather you should return the same view with the model so that the validation message gets binded.
return View(model); //model that the view expects..





或其他方式是如果modelState无效那么,

你可以使用



or another way is if modelState is not valid then,
you can use

ModelState.AddModelError("ModelPropertyName","Error Message")//if the data annotations are                      not used
return View(model);



我希望这有帮助。

如果有的话,回复你的查询。

谢谢。


I hope this helps.
Post back your queries if any.
Thanks.


这篇关于客户端验证不在Asp.net MVC4中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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