Changing a Model's value in httppost action - asp.net-mvc

I'm looking to change the value of a string in a view model in the controller and pass it back to the view like so:
[HttpPost]
public ActionResult TestAction(TestViewModel model)
{
model.TestStringValue = "test val";
return View("Index", model);
}
The value is shown in a textbox but isn't showing the updated value. What do I need to do in the controller to update a view model's field?

Try
ModelState.Clear();
To Clear Model State Values

Try
ModelState.Remove("TestStringValue");
model.TestStringValue = "test val";

Related

mvc c# return view() not defaulting to correct values

I am not sure if I am overlooking something obvious.
Once I do a POST, I have the following (Note: What I am trying to do is to default the same view with some null values so the user can create another entry):
[HttpPost]
public ActionResult QkInsert(ProgInfo model)
{
if (ModelState.IsValid)
{
ProgService.InsertQuickEntry(model);
model.Name = null;
model.Address = null;
model.Phone = null;
return view(model);
}
return view(model);
What is strange is that when I look at the value of model in the IsValid()
return view(model)
I do see the null values.
But when it is shown on the view, it is not null. It is basically the same view as when I had entered the data the first time. Any idea? Have I overlooked something?
Also notice how I have done return view(model) twice. Is there any other way of doing this to where I do it only once and not repeat?
That's because HTML helpers are first looking into the ModelState when binding their values and only after that the value in your model. This is by design.
So if you want to change any value of the model inside a POST action you need to remove it from the ModelState first:
[HttpPost]
public ActionResult QkInsert(ProgInfo model)
{
if (ModelState.IsValid)
{
ProgService.InsertQuickEntry(model);
ModelState.Remove("Name");
ModelState.Remove("Address");
ModelState.Remove("Phone");
model.Name = null;
model.Address = null;
model.Phone = null;
return view(model);
}
....
}
Now the view will render the modified values.
If the model is not valid you will return the same model, your second return.
There is no need at all for the first return view(model) as there is no code between it and the second one, so it will call anyway. That is, delete the first return and the logic is identical.

Accessing ViewBag in Controller that's set in a view

I understand I can do this using ModelBinding, but I'd like to know if I can use the ViewBag
I set the ViewBag property in the View
#{
ViewBag.WasPriorityCustomer = Model.PriorityCustomer == true;
}
A user who is not a priority customer can be changed to one, but i need to know if they were a priority customer to begin with.
In the controller
[HttpPost]
public ActionResult Save(MyModel model)
{
if (ViewBag.WasPriorityCustomer == false && model.PriorityCustomer == true)
{
//Thank you for becoming a priority customer
}
}
Unfortunately ViewBag.WasPriorityCustomer is always null
You just try to do things in the wrong way :
You can set a ViewBag's value in an action, and use it's value in the generated view.
But to get a value from a view in the POST action, you should use an hidden input.
something like that in the view (untested) :
# {bool wasPriorityCustomer = Model.PriorityCustomer;}
#Html.Hidden("wasPriorityCustomer", wasPriorityCustomer)
and Action becomes
[HttpPost]
public ActionResult Save(MyModel model, bool wasPriorityCustomer)
or you could change your ViewModel to include the "hidden" value.
and use #Html.HiddenFor(m => m.WasPriorityCustomer)
Try this code if you want to store some value in viewBag and want to use in this ViewBag value in View.
In Model Action
ViewBag.Test = ("NewTest");
in View you can write
#ViewBag.ProcedureTypeId
This way you can have ViewBag value in Viewpage

Why is my controller test not working, the view name is coming back empty

Doing a simple test to verify the view name for a controllers action:
var controller = new UserController();
var result = controller.Login() as ViewResult;
Assert.AreEqual("Login", result.ViewName);
The result.ViewName is coming back with "", why would this be?
Are you specifying the view name in the controller Login method or are you leaving it at the default value (which is "")?
If you leaving it at the default value, which is common, you need to test for String.Empty instead of "Login".
if you have a view like this
public ActionResult Index()
{
return View();
}
then the ViewName property will take it's default value that is "" and if you specify the view name like this it'll work fine
public ActionResult Index()
{
return View("Index");
}

aspnetmvc model is updated but doesn't update control

hejdig.
In Aspnetmvc2 I have a model object that I send to a View. A control in the view isn't updated with the value. Why? What obvious trap have I fallen in?
The View:
<%:Html.TextBox(
"MyNumber",
null == Model ? "1111" : Model.MyNumber ) %>
<%:Model.MyNumber%>
is first fetched trough a Get. The "1111" value in the textbox is manually updated to "2222". We post the form to the controller which appends "2222" to the Model object and sends it to the view again.
The Controller:
[HttpGet]
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index( MyModel myModel)
{
myModel.MyNumber += " 2222";
return View(myModel);
}
Alltogether we get an output like:
<input id="MyNumber" type="text" value="1111">
1111 2222
As you can see the control doesn't use the Model's attribute but instead falls back to thew viewstate that doesn't exist in Aspnetmvc.
(The same happens with Razor.)
That's normal and it is how HTML helpers work : they look first in the model state and then in the model when binding a value. So if you intend to modify some property in the POST action you need to remove it from the model state first or you will always get the old value:
[HttpPost]
public ActionResult Index(MyModel myModel)
{
ModelState.Remove("MyNumber");
myModel.MyNumber += " 2222";
return View(myModel);
}

model binding in mvc

i'm having a textbox inside a form.
[View]
<%=html.textbox("name") %>
[Controller]
Index(string name)
{
name = "something";
return View();
}
On Form Submit
In this case without sending any ViewData the textbox value is maintained.But the value "something" is not setting up.
But whn i change the Action to
[Controller]
Index()
{
string name="something";
return view();
}
the value is not maintained.
Really wat happening on that parameter.
If you want to set data for html.textbox("name") in the Controller use ViewData["name"] = "something"
Your question is not very clear and your code example is not actually adding anything to ViewData or the view Model - here's a shot at what i think your trying to do...
Assuming you want to re-populate the form and your View is Strongly Typed, You would do something like this:
public ActionResult Index(String name)
{
MyModel model = new MyModel;
model.Name = name;
ViewData.Model = model;
return View();
}
A textbox in your view with the same name would then have the value auto populated from the Model
<%= html.textbox("Name") %>
Posting the form would then post the model object to your controller like this:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(MyModel model)
{
// do something with the model
ViewData.Model = model;
return View();
}
and then re-populate the form with the model data.
string name in your Index action in the controller, is mapped to the FormValue, if you change this, MVC understands that it needs to add the value from the FormValueCollection to the textbox, and you have changed that in your Index action. If you declare a variable by yourself this doesn't work because there is no binding to the formvalues.

Resources