1

I have this problem:

I go to a page such as:

/Auction/Details/37

and this calls this action method:

public ActionResult Details(int id)

A particular line in this method is:

return View("DetailsLub", auction);

This view contains this line:

@Html.Action("BidOnAuction", new { auctionId = Model.Id })

Which calls this action method:

public PartialViewResult BidOnAuction(int auctionId)

So far so good?

Now, I have a form in the BidOnAuction view, whcih has a button. When I click on this button, this action method is invloked:

[HttpPost]
public ActionResult BidOnAuction(BidOnAuctionViewModel model)

This action method has a catch statement with the following lines:

ModelState.AddModelError(string.Empty, operation + @" Failure: " + message);
return RedirectToAction("Details", new { id = model.AuctionId });

Now, both the DetailsLUB view and the BidOnAction view contain this line:

@Html.ValidationSummary(true)

But, the issue is that nothing ever gets printed to the screen. What am I doing wrong?

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
Sachin Kainth
  • 45,256
  • 81
  • 201
  • 304
  • Did you solve this issue just by following the accepted awswer? I am having a similar issue using a partial view... If I use the partialview, the validation summary isn't displayed, but if I don't use it, the validations summary is shown as expected. I already have the accepted answer in my code... – amp Jul 29 '13 at 15:18

3 Answers3

2

InOrder to get the validation Message on the page you need to return view with Model, as model has the Model State within it, something like this:

return View(Model);

This will return the model BidOnAuction with Validation Summary.

Dhaval Shukla
  • 1,127
  • 2
  • 10
  • 19
1

This line of code

return RedirectToAction("Details", new { id = model.AuctionId });

Returns instance of RedirectResult class. That is generally used for redirections and does not render view. If you want to render child action into parent view using @Html.Action, you need to return view from that child action, not RedirectResult. And that RedirectResult will not work even when there's no child action. Returning RedirectResult causes browser to issue fresh, all new request to that action. And model state is lost anyways. You should do something like

try
{
    //some actions
    return RedirectResult("Details", new { id = model.AuctionId });
}
catch
{
    ModelState.AddModelError(string.Empty, operation + @" Failure: " + message);
    return View("Details", new { id = model.AuctionId });
}
archil
  • 39,013
  • 7
  • 65
  • 82
  • I already tried this, although it has to be a little different as there is no Details view. I have return View("DetailsEsf", auction). The problem is that this causes an infinite loop. The [HttpPost] public ActionResult BidOnAuction(BidOnAuctionViewModel model) action keeps getting called. – Sachin Kainth Jan 24 '12 at 13:02
  • Is there some way I can get it to you. There is not enough room in this text area. – Sachin Kainth Jan 24 '12 at 13:10
  • @archil I'm having a similar issue... Can you check this question please: http://stackoverflow.com/questions/17860803/validationsummary-inside-a-partial-view-not-showing-errors – amp Jul 29 '13 at 15:20
1

You can't redirect to a new action and expect the modelstate to be there.

If the modelState is invalid just return (with View(model)) else redirect to details.

If you need the error information in the details view you will have add it to TempData or pass it in as an optional parameter.

Lee Smith
  • 6,339
  • 6
  • 27
  • 34