MVC3 Dropdown repopulates fine even on error postback, but suggestion on how to make it efficient - without introducing error - asp.net-mvc-3

I can't post my code at the moment as I am on the road and out of office. Here is my code greatly simplified. I don't think I have a syntax issue as such - I think I need a clarification or, a pointer.
Model:
[Validaton attribute]
class Data
{
[Required]
int xx
..
}
ViewModel:
class VM
{
SelectItem Items;
int ItemID;
Data dt = new Data();
public VM
{
Items =... Load from db...
}
}
View
#Textbox xx (etc)
#DropDownFor(VM.ItemID, VM.Items
Controller
void Index
{
VM vm = new VM();
//I also default some items here works well - I get a nice form all loaded.
return View(vm);
}
[POST]
void Index(VM stuffPosted)
{
If ModelState.OK)(
{
All good. We direct to another page with Redirect To Action
}
else
{
return View(stuffPosted)
//This is the problem case where I need an efficiency
}
PROBLEM
The form loads fine with the Index method. On postback submit, the ID comes back correctly in ItemID. If there is an error - the ModelState sends back the view...and all the selection list(s) are in place, with selections. Oh joy! BUT, in the Postback method, the VM stuffposted instance class invokes the constructor and there is another (expensive) db call....to reload the collections..then the 'ViewState' seems to load the selected IDs over the top of it...
If I refactor the code so as there is no expensive reload of the collections...then I get the classic error along the lines of
int32 but expecting IEnumerable and my original form does not show ...
I am missing something, clearly..how to reload the collection from 'ViewState' (page) for reuse in an error situation, and not need a db call.

You could use SessionState to avoid the database trip to repopulate the dropdown. Populate the SessionState on the first GET request, and then load from it when you need to repopulate the dropdown (on error) on POST. However be wary of the size of the collection you are storing in SessionState - you may be better going to the database after all to re-load the data.

Related

Use the same instance of model view across views in ASP MVC 3

I have two views : the first one has a form which when submitted, it fills in a model view (QuizzModelView).
Now after submitting, I am redirected to another view which also has a form that I want to submit. The problem is that I want to use the same QuizzModelView for the two views. That means, when submitting the second form, I want also to submit the values of the previous form. I can do this by creating hidden inputs which take the values that come from the first view.
Is there a way to do it without hidden inputs.
Thanks
EDIT :
To explain more:
My model view contains : QuizzModelView.field1, QuizzModelView,.field2
1st step : View1 will fill in QuizzModelView.field1
2nd step : I am redirected to view2
3rd step : View2 will fill in QuizzModelView.field2
Now I want to be able to get QuizzModelView.field1 and QuizzModelView.field2. But I get Only QuizzModelView.field2 because QuizzModelView.field1 is lost when submitting View2
Here are my actions :
[HttpPost]
public ActionResult TAFPart2PopupEvents(QuizzModelView model)
{
return PartialView("PartialViews/_TAFPart2PopupEvents", model);
}
[HttpPost]
public ActionResult TAFPart3PopupEvents(QuizzModelView model)
{
// here I want to use
// model.field1 and model.field2
}
Technically (pedantically), you won't be able to use the same instance of the model. However, you can put it in the session and pass across the redirects. Session has the advantage of not getting tampered with as easily as hidden fields. Plus you wouldn't have to actually bind the whole model for each step - just the single field from each step:
[HttpPost]
public ActionResult TAFPart2PopupEvents(string field1)
{
QuizzModelView model = new QuizzModelView();
model.Field1 = field1
Session["Quiz"] = model;
return PartialView("PartialViews/_TAFPart2PopupEvents", model);
}
[HttpPost]
public ActionResult TAFPart3PopupEvents(string field2)
{
var model= (QuizzModelView )Session["Quiz"];
// Fill in field2 here
model.Field2 = field2;
}
Edit: To address Brian's comment with some actual detail -
This method with sessions is less susceptible to data tampering than hidden fields, if that's a concern at all. With hidden fields in the view, a malicious user could easily overwrite previous data. Depending on the size of your model, hidden fields could bloat up the view a bit too.
Sessions also have the drawback of expiring. Here's a simple way to handle expirations. If this is called via Ajax, then you'll have to pass an error message back to the client instead to handle there.
[HttpPost]
public ActionResult TAFPart3PopupEvents(string field2)
{
var model= Session["Quiz"] as QuizzModelView;
if (model == null)
{
// Add some kind of message here.
// TempData is like Session, but only persists across one request.
TempData["message"] = "Session Expired!";
return RedirectToAction("Index");
}
// Fill in field2 here
model.Field2 = field2;
....
}
If you want your TAFPart3PopupEvents action to have access to the data, you need to store it some place. There are many different options (session, querystring, db), but I think a hidden input (in general) is the easiest.

Storing model is session. Is there any better way for it?

I have a model class like following
public class ProductModel
{
string ProductName { get; set; }
int Quantity { get; set; }
}
In Controller I have an Action item
public ActionResult ShowProduct()
{
return View();
}
In my view user has two text boxes; where they enter product name and quantity. The first time they come in on this page these fields are empty. Once they enter values in these text boxes they hit a Next button which take them to a next page where they have to enter additional information about order.
On that page I have a back button and they can come back to this first page. Problem is I need to display the information that they entered in first page but on the second page I don’t have that ProductModel anymore. I can store that model in session but not sure if there is any better pattern of doing it in MVC
I would steer clear of Session and TempData. If you're using MVC, and your views are separated by full postbacks, (not Ajax) maybe you could use a view model pattern across different controller actions.
public class OrderController : Controller
{
public ActionResult ShowProduct()
{
return View(new ProductViewModel());
}
[HttpPost]
public ActionResult DoOrderStuff(ProductViewModel vm)
{
if (ModelState.IsValid)
{
// OrderViewModel would contain some product data
// to be used in the DoOrderStuff view
return View(new OrderViewModel(vm));
}
// error, go back to Page 1
return View("ShowProduct", vm);
}
}
This gives you room for validation while still following the wizard style views you described.
Caveat I just realized with this:
If you have a bunch of successive views, your user experience would probably suffer without a lot of hacking together of different view models. E.g. customer is on page 5 of the wizard, and wants to go back to page 2--my answer in its simplest form wouldn't accommodate that. However, with a good abstraction of the values in all your screens, it could be done.
This is pretty much what the Session dictionary was intended to be used for. You may look into using TempData but in essence it is just a lightweight version of Session. I don't see anything wroth with what you are doing.
I don't think you need to store this in the Session/TempData (watch out, how TempData works surprisingly changed quite a bit from MVC 2 to MVC 3). Your next button sounds like a POST, and then you do some sort of Redirect. If instead you made your form POST to the URL you wanted to display next, the ProductModel would be passed right along, and you could then pass it from the Action to the View, through either the Model or ViewData.

How do you exclude properties from binding when calling UpdateModel()?

I have a view model sent to the edit action of my controller. The ViewModel contains references to EntityObjects. (yea i'm fine with it and don't need to want to duplicate all the entities properties in the viewmodel).
I instantiate the view model and then call UpdateModel. I get an error that a property is "null" which is fine since it is a related model. I am trying to exclude the property from being bound during model binding. On debugging it I see in the entity where the model binder is trying to set the value of the property to null.
Here is my edit action:
var model = new SimplifiedCompanyViewModel(id);
var excludeProperties = new string[] {
"Entity.RetainedEarningsAccount.AccountNo"
,"Property.DiscountEarnedAccount.ExpenseCodeValue"
,"Entity.EntityAlternate.EntityID"
,"Property.BankAccount.BankAccountID"
,"Entity.PLSummaryAccount.AccountNo"
,"Property.RefundBank.BankAccountID"
,"Company.Transmitter.TCC"
};
try
{
UpdateModel<SimplifiedCompanyViewModel>(model, String.Empty, null, excludeProperties);
if (ModelState.IsValid)
{
//db.SaveChanges();
}
return RedirectToAction("Index");
}
catch
{
return View(model);
}
I have looked at a few other issues about specifying a "prefix" but I don't think that is the issue since I am telling it to bind to the viewmodel instance not just the entity object.
Am I excluding the properties correctly? Strange thing is is only seems to happen on this item. I suspect it may be an issue with the fact that there is actually no refund bank related to my entity. But I have other related items that don't exist and don't see the same issue.
More info... since I'm told me model isn't designed well.
The Company is related to a BankAccount. The Company view shows the currently related BankAccount.BankAccountId and there is a hidden field with the BankAccount.Key. I use jQueryUI autocomplete feature to provide a dropdown of bank account displaying the BankAccount.BankAccountId and when one is selected the jQuery code changes the hidden field to have the correct Key value. So, when this is posted I don't want the current bankaccounts BankAccountID modified, hence I want it to skip binding that field.
If I exclude BankAccountId in the model then on the BankAccount edit view the user would never be able to change the BankAccountId since it won't be bound. I'm not sure how this indicates a poor model design.
Use the Exclude property of the Bind attribute:
[Bind(Exclude="Id,SomeOtherProperty")]
public class SimplifiedCompanyViewModel
{
public int Id { get; set; }
// ...
}
This is part of the System.Web.Mvc namespace. It takes a comma-separated list of property names to exclude when binding.
Also you should consider using TryUpdateModel instead of UpdateModel. You can also just have the default model binder figure it out by passing it as an argument to the constructor:
public ActionResult Create([Bind(Exclude="Id")]SimplifiedCompanyViewModel model)
{
// ...
}
A very simple solution that I figured out.
try
{
UpdateModel<SimplifiedCompanyViewModel>(model, String.Empty, null, excludeProperties);
ModelState.Remove("Entity.RetainedEarningsAccount.AccountNo");
ModelState.Remove("Property.DiscountEarnedAccount.ExpenseCodeValue");
ModelState.Remove("Entity.EntityAlternate.EntityID");
ModelState.Remove("Property.BankAccount.BankAccountID");
ModelState.Remove("Entity.PLSummaryAccount.AccountNo");
ModelState.Remove("Property.RefundBank.BankAccountID");
ModelState.Remove("ompany.Transmitter.TCC");
if (ModelState.IsValid)
{
//db.SaveChanges();
}
return RedirectToAction("Index");
}
catch
{
return View(model);
}
Another option here is simply don't include this attribute in your view and it won't be bound. Yes - you are still open to model injection then if someone creates it on the page but it is another alternative. The default templates in MVC will create your EditorFor, etc as separate items so you can just remove them. This prevents you from using a single line view editor with EditorForModel, but the templates don't generate it that way for you anyways.
EDIT (adding above comment)
DRY generally applies to logic, not to view models. One view = one view model. Use automapper to easily map between them. Jimmy Bogard has a great attribute for this that makes it almost automatic - ie you create the view model, load up your Customer entity for example, and return it in the action method. The AutpMap attribute will then convert it to a ViewModel. See lostechies.com/jimmybogard/2009/06/30/how-we-do-mvc-view-models
Try the Exclude attribute.
I admit that I haven't ever used it.
[Exclude]
public Entity Name {get; set;}

How do I pass an object from the Index view to the edit view using MVC3

I have created a simple WCF service that is to be configured by an MVC3 UI.
When I call the index page from my controller, I want to display the values held in the configuration, which has been returned by the service. The user could then chose to edit these settings and then send them back to the service.
I want to do something like this in the index view ...
<div>
#Html.ActionLink("Edit", "Edit", model)
</div>
and then consume the model in the controller like this...
[HttpPost]
public ActionResult Edit( SettingsModel Config)
{
try
{
List<string> configErrors = null;
if (ModelState.IsValid)
{
// Set up a channel factory to use the webHTTPBinding
using (WebChannelFactory<IChangeService> serviceChannel = new WebChannelFactory<IChangeService>(new Uri(baseServiceUrl)))
{
IChangeService channel = serviceChannel.CreateChannel();
configErrors = channel.SetSysConfig(Config);
}
}
return RedirectToAction("Index");
}
catch
{
return View();
}
}
but this doesn't work.
Any suggestions???
When the form gets posted, all the input type fields data is collected and sent to the server. You can see this data using FireBug. The key point here is that, is the data that is being posted in a form, that MVC's default model binder can understand and map it to the model object, which is being passed as input parameter to the action method.
In your case, the model is of type "SettingsModel". You have to ensure that, the form data that is being posted is in format, that can be mapped to the "SettingsModel" object.
Same kind of question discussed in another thread : Can't figure out why model is null on postback?
Check Out this article : NerdDinner Step 6: ViewData and ViewModel
In the above article, carefully go through the "Using a ViewModel Pattern" section. My guess is that, this is what you are looking for.
You will need to post the values to populate the SettingsModel object on the Edit action. You can do this using hidden form fields if you don't want the user to see it. Otherwise you could have no parameters on the Edit action and make another call to the web service to populate the Settings model.

MVC - Dealing with the model and events on the page

I have what is probably a basic question regarding how to structure an MVC page.
Assume this is my model:
public class MyModel
{
int ProductId
List<ParameterTable> ParameterTables
...
[other properties]
...
}
ProductId initially won't have a value, but when its value is selected from a DropDownList it will trigger an event that retrieves the List items associated with that product.
My problem is when I do this AJAX call to get the parameter tables I'm not sure how to handle the response. I've only seen examples where people then manually inserted this data into the page via the jquery. This would mean handling displaying the data in your view (for the first time loading the page) and in the jquery (whenever it changes).
I am wondering if there's a way to somehow pass back a model of sorts that binds my return value of List into my page without needing to specify what to do with each value.
Would I have to have the changing of the ProductId DropDownList trigger an ActionResult that would reload the whole page to do this instead of a JsonResult?
You could return a partial view with your ajax call.
Controller action:
public ActionResult Filter(int productId) {
var product = _repository.Find(productId);
if (Request.IsAjaxRequest())
{
return PartialView("_Product", product);
}
else
{
return View(product);
}
}

Resources