本文介绍了客户端验证不在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");
        }
}

推荐答案

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中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 05:30
查看更多