Rajeev Kumar February 2016

Model Error Not Showing after Http Post MVC 4

I am validating date input server side and adding ModelError if user input is invalid. Following is my code

public ActionResult EditOffer()
    {
        var offerID = Convert.ToInt64(Request.RequestContext.RouteData.Values["id"]);
        using (joyryde_storeEntities context = new joyryde_storeEntities())
        {
            var objOffer = context.tbl_offer.Where(x => x.LNG_OFFER_ID == offerID).FirstOrDefault();
            ViewBag.OfferID = offerID;
            ViewBag.Header = "Edit " + objOffer.TXT_OFFER_TITLE;
            ViewBag.ActionToPerform = "Edit";
            if (System.IO.File.Exists(Server.MapPath(string.Format("~/assets/images/Stores/{0}/O_{1}_Small.jpg", Session["StoreID"], offerID))))
            {
                objOffer.TXT_OFFER_SMALL_PATH = string.Format("~/assets/images/Stores/{0}/O_{1}_Small.jpg", Session["StoreID"], offerID);
            }
            return View("AddOffer", objOffer);
        }       
    }
    [HttpPost]
    public ActionResult EditOffer(tbl_offer modal, string Add, string Edit)
    {
        if (ModelState.IsValid)
        {
            using (joyryde_storeEntities context = new joyryde_storeEntities())
            {
                var offerID = Convert.ToInt64(Request.RequestContext.RouteData.Values["id"]);
                if (!isOfferExist(modal.DAT_START_OFFER.Value.Date, modal.DAT_END_OFFER.Value.Date.AddHours(23).AddMinutes(59).AddSeconds(59).AddMilliseconds(999), Convert.ToInt64(Session["StoreID"]), offerID, Add, Edit, context))
                {
                    // My Code 
                    return RedirectToAction("AllOffers", "Store");
                }
                else
                {
                    ModelState.AddModelError("DAT_START_OFFER", "Date Not Available"); // Here i am adding Modal Error For Date
                    if (Edit != null)
                    {
                        return RedirectToAction("EditOffer");
                    }
                   

Answers


Marcin Iwanowski February 2016

You didn't see any error because of RedirectToAction. You should use the "View" method. For example you can just write return EditOffer()


Sousuke February 2016

When you set your model error then you use RedirectToAction, what mean that you load new page, check in your debug, after that your code go back to GET method and everything is reloaded. You have to return your View with model.

ModelState.AddModelError("DAT_START_OFFER", "Date Not Available");
if (Edit != null)
{
    return View(modal); //if your model is object named modal
}


anand February 2016

If you use return RedirectToAction("EditOffer"); the error will not been show it will be redirect to public ActionResult EditOffer(){} Action method, the [HttpGet] will be shown.
To rectify this error, you should use View() method. like return View(); it return the error to the form data posted page.

    [HttpPost]
public ActionResult EditOffer(tbl_offer modal, string Add, string Edit)
{
    if (ModelState.IsValid)
    {
        using (joyryde_storeEntities context = new joyryde_storeEntities())
        {
            var offerID = Convert.ToInt64(Request.RequestContext.RouteData.Values["id"]);
            if (!isOfferExist(modal.DAT_START_OFFER.Value.Date, modal.DAT_END_OFFER.Value.Date.AddHours(23).AddMinutes(59).AddSeconds(59).AddMilliseconds(999), Convert.ToInt64(Session["StoreID"]), offerID, Add, Edit, context))
            {
                // My Code 
                return RedirectToAction("AllOffers", "Store");
            }
            else
            {
                ModelState.AddModelError("DAT_START_OFFER", "Date Not Available"); // Here i am adding Modal Error For Date
                if (Edit != null)
                {
                    return View(modal);
                }
                else
                {
                    return RedirectToAction("AddOffer");
                }
            }

        }
    }
    else
    {
        ViewBag.OfferID = Here give the office id;
        ViewBag.Header = "Edit " + objOffer.TXT_OFFER_TITLE;
        ViewBag.ActionToPerform = "Edit";
        ModelState.AddModelError("","Your Error Message"); // Here i am adding Modal Error For Date
        return View(modal);
    }
}

Post Status

Asked in February 2016
Viewed 1,193 times
Voted 9
Answered 3 times

Search




Leave an answer