How can ViewBag data be saved after a form post? - asp.net-mvc

So I have a ViewBag.Something and this data is randomly generated. In my view, I set this to a label like so #Html.LabelFor(m => m.Something, (string)ViewBag.Something). This works out well but when I submit the form there could be errors and if there are errors, I need this label to remain the same, I don't want dynamic/random data anymore so I wouldn't call the controller method that generated this ViewBag. Is there a way to retain this value without having some private variable in my controller? Some nice way that MVC/Razor does it?

Option 1:
Pass the value of "ViewBag.Something" to the Controller by using route Values:
#Html.ActionLink("ButtonText", "ActionName", new { Something = #ViewBag.Something })
Option 2:
You can use TempData.
public ActionResult Index()
{
var Something = "YOURVALUE";
TempData["Something"] = Something;
.......
}
public ActionResult OtherAction()
{
var Something = TempData["Something "];
...........
}
Passing State Between Action Methods
Action methods might have to pass data to another action, such as if an error occurs when a form is being posted, or if the method must redirect to additional methods, as might occur when the user is directed to a login view and then back to the original action method.
An action method can store data in the controller's TempDataDictionary object before it calls the controller's RedirectToAction method to invoke the next action. The TempData property value is stored in session state. Any action method that is called after the TempDataDictionary value is set can get values from the object and then process or display them. The value of TempData persists until it is read or until the session times out. Persisting TempData in this way enables scenarios such as redirection, because the values in TempData are available beyond a single request.

In the get, set up a model, set it dynamically and when return view() is being executed, do return view(model). Then in the view, set up a hidden field that can keep on passing the value needed. I chose to go this route because I don't have to worry about any server code to make this work on a post and I don't have to worry about any sessions.

Displaying message in viewbag after posting (I user this method)
ActionResult SubmitUser(){
ViewBag.Msg =TempData["Msg"];
return view();
}
[HtttpPost]
ActionResult SubmitUser(){
TempData["Msg"] ="Submitted Successfully"];
return view();
}
The Value is send to get method..

Related

Working with tempdata in mvc

As I have read that once you set a tempdata, it will last till next request and if you need to use it more then we need to use keep. But in this situation there is somewhere my tempdata is being lost.
The scenario is below:
I have a view and corresponding to that I have a action method in my controller and here I just set a tempdata as below:
Controller class:
public actionresult myview()
{
tempdata["Empid"]= sourceid;
}
The view consists of several renderAction as below and all these actionmethods in controller return some partialviews
#html.renderAction("details","mycontroller")
#html.renderAction("details","mycontroller")
#html.renderAction("details","mycontroller")
#html.renderAction("details","mycontroller")
Now in partialviews, I have several ajaxified calls suppose in one of the partialview , I have a post method using ajax like below:
$.post("action", "controller",{}});
Please ignore syntactical mistakes as I only have problem in understating the logic of tempdata.
So, coming to above, now the post methods have actionmethods that uses the tempdata that i have set when my view page loads i.e tempdata["Empid"] because this tempdata is needed to get details of employee.
So, it gets the data, reurn json formatted data and I show it in view perfectly.
Uptill here everything is fine. Now, I have one of the partialviews that uses the ajax post to hit controller which is not the same controller but a different cs file. Here this tempdata goes off.
Why is it happening so..
TempData is designed for a short life by default. So the data you set to TempDataDictionary object persists only from one request to the next request. After that it is not going to be available.
You can use the TempDataDictionary.Keep() method to persist the value for the next request again.
public ActionResult Index()
{
TempData["Message"]="Hello";
return View();
}
public ActionResult GetCustomer()
{
var msg = TempData["Message"] as string;
TempData.Keep("Message");
return View();
}

Refreshing ViewData in a method which returns Json

I have a controller with a method which returns a partial view and generates ViewData. Then, I have some methods, each of them returns Json objects and ViewData. But, the ViewData is not getting refreshed. How to refresh it? Is this possible?
public ActionResult FirstMethod() {
ViewData["someList"] = ...;
return PartialView(someOtherList);
}
public JsonResult SomeMethod()
{
ViewData["someNewList"] = ...;
return new JsonResult { ... };
}
But, although SomeMethod() is called after FirstMethod(), the ViewData which I use in my view is someList.
Is this possible?
No, it's not possible. The ViewData is a weakly typed dictionary which could be used (although I wouldn't recommend using it) to pass information between a controller and a view. Its lifetime is tied to that of the controller action execution pipeline. So once the view is rendered it's over. No controller, view, ViewData, ... exists anymore. Only HTML rendered in the client browser.
Then you send an AJAX request to the server again. This is completely new request that has nothing to do with the first one (which was used to render the view initially) and thus a new instance of the controller is created with its own ViewData. Setting ViewData in a controller action that returns JSON is useless because since this action will be invoked with javascript, all the information that you want to pass from the controller to the javascript success handler must be part of the JSON object that you return.

How to re-use model data on post back with MVC

I'm passing structured data to my MVC page when it loads initially. After the user submits a contact form, I want to re-use the same data (I just "leave" the same page up) so I don't have to hit the database again. I declared a variable global to the controller to store the model data, but it's null at the end of the post back, so it looks like I can't re-use it there.
Seems like this would be a typical scenario. How do I handle it?
If you are wanting to reuse viewmodel or other retrieved data that is not going to be part of the postback, you can either
a) Output it in hidden fields so that it is posted back to your action (meh) or
b) Store the object(s) in Session so that it will be available to any other controllers/actions in your application. If you are worried about memory, you could delete that session variable after you reuse it if you are not going to need to use it again.
On your initial page load, check if the session variable exists, if it does, you are good - else populate it.
Oh and why the global variable thing isn't working -> a controller is new'd up for each request (assuming using the default controller factory) and as such any global variables in the controller will be reset on each request.
public ActionResult Foo()
{
var model = GetModelFromDB();
Return View(model);
}
[HttpPost]
public ActionResult Foo(Entity model)
{
Return View(model);
}
Asp.net-mvc is stateless so each HTTP request has a different context, and each time you hit the controller all it's data reset in the constructor, this why you get null.
You can get the model in the post if it's properties are within the submitted form .
If you really don't want to leave the page you are on, and don't want to post all the other data back as KMan suggests, but still want to capture the users contact information/data you could post the contact info using ajax.
If you have your view model as an argument to your method, you can just return it to the view on postback. Ex:
public ActionResult TestAction(MyViewModelType testViewModel)
{
//Do logic
return View("view",testViewModel);
}
Note that you have to have the data inside the form you are posting.

RedirectToAction not passing all parameters

I have these two methods in the same Controller, the first passing two parameters to the second.
When debugging, the list (model.RequestedProducts) passed is correct (not empty) but on the second method, only idOR is read correctly, List<OCS> RequestedProducts is empty.
[HttpPost]
public ActionResult Index(int idOR, ViewModel model, string add, string remove, string send)
{
//...
return RedirectToAction("Done",
new { idOR = idOR,
RequestedProducts = model.RequestedProducts});
}
public ActionResult Done(int IdOR, List<OCS> RequestedProducts)
{ ...
What am I missing?
Is there maybe a better way to do it? (other than Redirect to Action)
Thank you
When you use RedirectToAction you are returning a message to the client to request a new URL probably something like /controller/action/id. You Routes will define how the URL is formed. I am guessing you have the default route defined and in your case MVC has no way of knowing how to deserialise your RequestedProducts type into a URL and then bind it back into a List type.
Instead you could use the TempData object to pass data between to Action requests.
The TempData property value is stored in session state. Any action method that is called after the TempDataDictionary value is set can get values from the object and then process or display them. The value of TempData persists until it is read or until the session times out.
This MSDN article explains it all.

ASP.NET MVC Unbind Action Parameter

Is it possible to disable a certain action parameter from retaining its value across requests?
[HttpPost]
public ActionResult MyAction(string value1, string value2)
{
if(value1=="hi")
ModelState.AddModelError("value1", "Can't have hi");
//do stuff
if(ModelState.IsValid)
return RedirectToAction("Finish");
else
return View()
}
[HttpGet]
public ActionResult MyAction()
{
return View()
}
The view consists of a simple form with two input boxes (value1 and value2). Once submitted and validation fails, the view is returned. I want to always have the value of the textbox in the view to be empty.
The value for the textbox "value1" is retained if the the model is invalidated.
I tried to declare the textbox as <%= Html.TextBox("value1", null) %> but the value is still retained. I also tried to use [Bind(Exclude="value1")] but that dosen't work on a single variable.
Update 2:
I'm doing this for a textbox that is used for Captcha (custom solution) input. I want the textbox to be cleared any time the page is loaded, but I want validation to remain.
Try calling
ModelState["value1"].Value
= new ValueProviderResult(null, string.Empty, CultureInfo.InvariantCulture);
before you return the view from within your controller action.
What this does is keep all the errors associated with the key "value1", but replaces the value with an empty value.
What are you doing that's causing it to be retained? There isn't anything like ViewState in MVC that will persist a value over multiple requests unless you're writing code or using form fields to make it do so.
What does the view look like? Is this action method being called via GET or POST? What's the "do stuff" contained in your method?
Edit: You're still showing //do stuff in your example code. Does that stuff contain any references to ViewData? Your question is about binding, but I don't see any binding happening. Maybe this is beyond my understanding.
Edit 2: Glad Phil saw this one! The original question didn't mention the ModelState.

Resources