Click here to Skip to main content
13,861,916 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
Hi..
I am new to MVC 4.
I am developing a simple application in MVC 4 using Razor in c#.
Now i need to Display a error in view which is pass from controller?
How can i achieve it?

MY CODES:
MY VIEW CODE:
-------------
@model mvc_cs.Models.FormModels
@using ctrlr = mvc_cs.Controllers.FormController
@{
    ViewBag.Title = "form_edit";
    Layout = "~/Views/Shared/_Layout.cshtml";
    
}
<h2>
    form_edit</h2>


@using (Html.BeginForm("form_edit", "Form", FormMethod.Post))
{
    
        <table>
        <tr>
        <td>
          @Html.DisplayTextFor(model => model.error_msg)
          @Html.ValidationSummary(false)
      
        </td>
        </tr>
        <tr>
        <th>
            @Html.DisplayNameFor(model => model.content_name)
            @Html.DropDownListFor(x => x.selectedvalue, new SelectList(Model.Countries, Model.dd_value, Model.dd_text), "-- Select Product--")   
        </th>
        </tr>
</table>
    
<table>
    <tr>
        <td>
            <input  type="submit" value="Submit" />
        </td>
    </tr>
</table>
}


MY CONTROLLER:
--------------
[HttpPost]
      public ActionResult form_edit(FormModels model)
      {
          model.error_msg = model.update_content(model);

          TempData["error"] = "fjghsfhgflsh";
          ModelState.AddModelError("error.error", "adfdghdghgdhgdhdgda");
          
          return RedirectToAction("Form_edit", "Form",model.error_msg);
        
      }
Posted
Updated 8-Nov-13 2:32am
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

You can send your error as above using TemData for the next action method.Inside the redirected action method use ViewBag for put above error and show it to the view as below.

Action Method

public ActionResult Form_edit()
{
    ViewBag.Error = TempData["error"];
    return View();
}



View

@ViewBag.Error


Check this article for more options : What is ViewData, ViewBag and TempData? – MVC options for passing data between current and subsequent request[^]
   
v2
Comments
srameshsathy 21-Nov-13 6:47am
   
Sorry,This not works for me.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Hi,

You can use
@if (!ViewData.ModelState.IsValid)
{
      <div>Error description</div>
}

In controller
if (yourError)
{
   ModelState.AddModelError("Error", "Ex: This login failed");
   return View();
}

and in View page
@Html.ValidationMessage("Error")


Hope this helps you a bit.

Regards,
RK
   
Comments
Sampath Lokuge 9-Nov-13 8:37am
   
The problem your solution having is you didn't give an answer for the Op's question. If you watch closely you can see that he uses the 'RedirectToAction("Form_edit", "Form",model.error_msg)' inside the Action method.So for such a situation you have to carrying data by using TempData (or ViewModel) into next Action Method.Your solution is suitable when need to send data directly from Action to View.
♥…ЯҠ…♥ 9-Nov-13 8:42am
   
I Agree with you.. before redirecting Op's assigning error string to AddModelError so my solution suitable for the question I believe.
Sampath Lokuge 9-Nov-13 8:53am
   
Nope,It won't work.'AddModelError' works only with the Action to View.You cannot use it to carrying data into another Action where the way Op needs.
srameshsathy 25-Nov-13 2:19am
   
ya... u r correct it seems. Can u help me to tackle this ???
srameshsathy 22-Nov-13 0:08am
   
None worked for me..
What i done is:
Controller:
[HttpPost]
public ActionResult form_edit(FormModels model)
{
model.error_msg = model.update_content(model);
ModelState.AddModelError("error", "adfdghdghgdhgdhdgda");
ViewBag.error = TempData["error"];
return RedirectToAction("Form_edit", "Form",model.error_msg);

}
AND MY VIEW:
@using (Html.BeginForm("form_edit", "Form", FormMethod.Post))
{

<table>
<tr>
<td>
@Html.DisplayTextFor(model => model.error_msg)
@Html.ValidationSummary("error")
@ViewBag.error
@Html.ValidationMessage("error")

</td>
</tr>
<tr>
<th>
@Html.DisplayNameFor(model => model.content_name)
@Html.DropDownListFor(x => x.selectedvalue, new SelectList(Model.Countries, Model.dd_value, Model.dd_text), "-- Select Product--")

</th>
</tr>
</table>

<table>
<tr>
<td>
<input type="submit" value="Submit" />
</td>
</tr>
</table>
}
Member 10243803 17-Feb-14 7:57am
   
how to do java script validation in mvc4 cshtml
♥…ЯҠ…♥ 17-Feb-14 8:02am
   
You could use JQuery for that

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web01 | 2.8.190214.1 | Last Updated 9 Nov 2013
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100