Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
3.00/5 (5 votes)
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:
-------------
C#
@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:
--------------
C#
[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 1:32am
v2

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

SQL
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[^]
 
Share this answer
 
v2
Comments
srameshsathy 21-Nov-13 6:47am    
Sorry,This not works for me.
Hi,

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

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

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


Hope this helps you a bit.

Regards,
RK
 
Share this answer
 
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>
}

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