I need some information here. I am totally new at MVC therefore for you guys, I think it will be an easy question to answer. I have the following structure:
Controller.cs
Public ActionResult PageMain() {
return View(); // this is the main page I'm working with
}
[ChildActionOnly]
Public PartialViewResult Partial1(string tablename) {
//Some code to construct datatable according to the url parameter
return PartialView("Partial1", DataTable);
}
Public ActionResult FormAction(string tablename, FormCollection formvalues) {
//Here I send the values to the model in which I have a public void
//that updates the database -> I'm not using Linq at this phase because
//both the tables and fields are dynamic
//I execute the code in a try and catch statement
try
{
//some code
Response.Redirect("url to PageMain");
}
catch (Exception ex) {
ModelState.AddModelError("Error", ex);
Return View("PageMain", ex);
// actually, here I'd like to send the exception error
// to the partialview which renders the error as its model but
// but I don't know how since if I use return PartialView()
// then only the partial view will be displayed, not the whole page
}
}
Finally, in the PageMain View I have:
//Some initial code with the form that posts value to FormAction
@Html.RenderPartial("Partial1") //this is the partial which shows error
//it is only displayed when the form is posted
//and there is an error
Okay, now, my questions are: Is such a structure valid (here by valid I mean if it is well structured or there is a better way)? and how can I reach the Exception in the ModelState.AddModelError() method in the Partial View ‘Partial1’?
In case you are confused, to sum up:
- In PageMain, there is a table constructed in accordance with the url-parameter. Actually,
it is constructed in another partialview but displayed in the PageMain - When I edit the table, the form redirects me to FormAction, which the codes are executed
to edit the database - Finally, if there is error, user remains in the FormAction, but in this page the View used is still PageMain, I have not a different view for this page since it would be like constructing the same page twice. I mean, only to include a partialview which shows the errors I did not want to create another view. Instead, I am trying to make visible the partial view with some if – else logic only in case of some errors
A few things I would change here
First, here:
Response.Redirect("url to PageMain");You want to instead return a
RedirectToAction("PageMain")second – make Pagemain only valid for get requests by using the HttpGet attribute.
[HttpGet] public actionResult PageMain() { return View(); // this is the main page I'm working with }Third – make this HttpPost
Fourth –
Usually you will see people have the GET method and POST method have the same name, and one is marked HttpGet and one HttpPost accepting of course different parameter types.
Fifth –
What I would recommend is your view is a strongly typed view not based around DataTable but your own class – say named “Customer”
at the top of your view you know its strongly typed when you see something like (for a list of customers)
When you do this, your FormAction method can automatically take an object of type Customer – the Model Binder in MVC automatically matches your form values to names in this object and sets the property values. This is one of the great features with MVC. So your method would become:
and now you have a customer object to deal with.