I have a situation where i have to take input(form) from user. After continue button is pressed next view page is displayed. But after continue is pressed i don't want to store the model in the DB. I have to display some details(combining some tables) according to input given by the user earlier and again get some data from user. Only then i want to store the model in the respective tables.
How can i perform this? I tried getting Model from user and passing to the function that generates next page. Is this is way to do it? or there is other way around?
Store the model submitted by the first form in session.
[HttpPost]
public ActionResult ContinueForm1(Model1 model1)
{
if(ModelState.IsValid)
{
Session["Model1"] = model1;
return View("Form2");
}
return View();
}
[HttpPost]
public ActionResult ContinueForm2(Model2 model2)
{
if(ModelState.IsValid)
{
... model2 is already here, get the model1 from session
... and save to datatbase finally return a different view or redirect to some
... other action
}
return View();
}
You are heading down the right track.
You need to grab the model that is passed back from the first view - preferably you are using ViewModels here rather than binding directly to your db models. Have a look at http://lostechies.com/jimmybogard/2009/06/30/how-we-do-mvc-view-models/ and Why should I use view models? as to why these are good things.
The easiest way to do this is to pass the model in as an argument to your method e.g.
Assuming that your views are using the same ViewModel ( which may or may not be true) then you can send the viewmodel straight to your new view - else you can copy the elements into a new viewModel and send that.
e.g.
[HttpPost]
public ViewResult Step1(MyViewModel viewModel)
{
//Do some validation here perhaps
MySecondViewModel secondViewModel = new MySecondViewModel{
Id = viewModel.Id,
// etc. etc.
};
return View("Step2", secondViewModel);
}
Then you can carry on as you need until you have to persist the entity to the database.
NB as you do not need to do anything special in the form to make it post the model as an argument as long as the view is strongly typed to that ViewModel.
Related
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.
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.
Updating an object with MVC3
I have a model that I can modify, please see the sample below:
[HttpPost]
public ActionResult Edit(Company c)
{
if (ModelState.IsValid)
{
db.Entry(c).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(c);
}
The model has other fields that are not showing in the view and cannot be modified by the user, but when I click the submit button the fields that were not showing in the view got set to null.
Can I somehow let EF know not to modify certain fields? Thanks.
Generally it is better not to bind to the entity object directly, rather create an edit model and bind to that.
After all.. whats to stop someone posting back values you don't want changed with this approach?
The main problem here is the fact that mvc model binding changes the properties in the model before its in a context therefore the entity framework doesn't know which values have changed (and hence which should be updated)
You've mitigated that slightly with db.Entry(c).State = EntityState.Modified; but that tells the entity framework that the whole record has been updated.
I would normally do the following:
Bind to a model specifically for this controller first
Create an instance of the entity class you want to update, set the Id accordingly and attach it to the context
Update the properties on the entity to be the same as the model you binded to (object is attached and therefore entity framework is tracking which columns are being changed now)
SaveChanges
Step 3 is a bit tedious therefore consider using a tool like automapper to make things easier
Edit:
[HttpPost]
public ActionResult Edit(Company c)
{
if (ModelState.IsValid)
{
Company dbCompayObjct = new Company { companyId = c.companyId };
db.Company.Attach(dbCompayObjct);
dbCompanyObjct.CompanyName = c.CompanyName;
dbCompanyObjct.City = c.City;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(c);
}
You are apparently overwriting your existing record with an incomplete record. When you use the method above, it will completely replace the existing one.
You either need to fill in all the fields you don't want to replace with the existing values, or you need to get the existing record and modify the fields you want to modify, then save it.
Reflection is not always evil, sometimes it's your friend:
public ActionResult Edit(Company c)
{
if (ModelState.IsValid)
{
Company UpdateC = db.Company.find(c.CompanyID);
foreach (var property in typeof(Company).GetProperties())
{
var propval = property.GetValue(c);
if (propval != null)
{
property.SetValue(UpdateC, propval);
}
}
db.SaveChanges();
return RedirectToAction("Index");
}
return View(c);
}
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.
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);
}
}