Click here to Skip to main content
15,886,774 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Following Is Model named : AlbumData

C#
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
C#
@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>

}



Next Is My Controller named :AlbumCreationController

C#
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");
        }
}
Posted
Updated 8-Dec-14 20:41pm
v3

1 solution

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.
C#
return View(model); //model that the view expects..


or another way is if modelState is not valid then,
you can use
C#
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900