0

In Asp.net MVC ,I just want to return a ModelObject to View after a postback from[HttpPost]ActionMethod, but the condtion is that ModelObject should be Same/SameType of ModelObject-Argument used in same [HttpPost]ActionMethod.

Issue :After the post back NULL ModelObjectAttributes are shown in the same view.

OtherFindings:while removing the ModelObject from the argument of [HttpPost]ActionMethod and after postbacking ,the values are shown properly in the view

Code is attached.

public ActionResult Index()
{
    //TestModelClass objModel = new TestModelClass();
    objModel.name = "I am from GET";
    objModel.message = "GET Message successfully delivered";
    return View(objModel);
}

ISSUE : This Action Method is not working.ie; "No values are shown at View" OtherFindings :Values are only shown in the View, while we are not using the "TestModelClass objModel" Argument.

[HttpPost]
[ActionName("Index")]
public ActionResult IndexPost(TestModelClass objModel)
{
    enter code hereTestModelClass objModel1 = new TestModelClass();
    objModel1.name = "I am from POST";
    objModel1.message = "Post Message successfully delivered";
    return View("Index",objModel1);

}

Model

public class TestModelClass
{
    public string name { get; set; }
    public string message { get; set; }
}

View

@model testApp.Models.TestModelClass
@using (Html.BeginForm("Index","Home"))
{
    <table> 
        <tr>
            **@Html.TextBoxFor(x=>x.name)**
        </tr>
        <tr>
            **@Html.TextBoxFor(x=>x.message)**
        </tr>
        <tr>
            <input type="submit"name="submit",value="ENTER">
        </tr>
    </table>
} 

kindly check this issue and inform any other valid solutions are available. thanks in advance

Mayank Jain
  • 5,663
  • 7
  • 32
  • 65
  • That's by design. When you post, the model is bound and its values are added to model state. When you return the view, the helpers use the values from model state, not from the properties. You need to follow the PRG pattern (i.e redirect back to the `Index()` method. –  Jun 25 '15 at 05:50
  • The reason for this behavior is explained in the second part of [this answer](http://stackoverflow.com/questions/26654862/textboxfor-displaying-initial-value-not-the-value-updated-from-code/26664111#26664111) –  Jun 25 '15 at 05:51
  • Won't it work `@Html.TextBoxFor(x=>x.name, new { @Value = x => x.name})`? – Imad Jun 25 '15 at 05:54
  • @ImadoddinIbnAlauddin, That breaks the standard MVC behavior. DONT do it! –  Jun 25 '15 at 05:55
  • That means we have to use viewbag or alike to pass data? – Imad Jun 25 '15 at 05:57
  • 1
    @ImadoddinIbnAlauddin, No, using a model is the correct approach. OP's issue is that typeof `TestModelClass` is posted and its values are added to `ModelState` - e.g. it will include `name: "I am from GET"` Creating a new instance of `TestModelClass` in the POST method and assigning new values is ignored because the html helpers use the values from `ModelState` so `@Html.TextBoxFor(x => x.name)` will render `"I am from GET"` when the view is returned, not `"I am from POST"`. See the link in my earlier comment for why this is the default behavior. –  Jun 25 '15 at 06:05
  • ,thanks for this spark of code, **"Html.TextBoxFor(x=>x.name, new { @Value = x => x.name})" **but unfortunetly it dosent work like that. To make work this code ,i had made some changes ie ;" **@Html.TextBoxFor(x => x.name, new {@Value=@Model.name})**" –  Jun 25 '15 at 07:48
  • @Stephen Muecke thanks for this **"ModelState" behavior** , i guess i need to use **return RedirectToAction("Index");** ( Index get method ), i had tried **ModelState.clear() method in the IndexPost and it is working** - –  Jun 25 '15 at 07:49
  • @Stephen Muecke : can you **explain/Clarify what's the use of this behavior in MVC** , other than explaining about **using String value in integer field** ,which is mentioned in earlier Post[Link](http://stackoverflow.com/questions/26654862/textboxfor-displaying-initial-value-not-the-value-updated-from-code/26664111#26664111) –  Jun 25 '15 at 07:49
  • 1
    That is the reason why this is the default behavior. If a user posts back an invalid value, then that same (invalid) value is displayed when you return the view (along with the error message) so it can be corrected. If you wanting to display a different model with different values, then you should be redirecting to another method to display a new view (although you could also solve it in your case by adding `ModelState.Clear()` as the first line in your POST method - but this also clears all errors as well so can have unintended consequences) –  Jun 25 '15 at 07:56
  • `ModelState.Clear()` (as per above comments) is the way to go. This is because, when the view is generated, it will use matching values from the `Request` (stored in the `ModelState`) which are picked up _after_ the `return View(...`, so you don't get to change them. If you want full control over what goes back, clear these (with `ModelState.Clear()`) and provide everything your view needs. – freedomn-m Jun 25 '15 at 08:56
  • @StephenMuecke,OK thanks for the clarification, i have another doubt based on your statement "you could also solve it in your case by adding ModelState.Clear() as the first line in your POST method - **but this also clears all errors as well so can have unintended consequences"** –  Jun 25 '15 at 09:29
  • In a Scenario where user can give as input from a view and corresponding controller will give corresponding to the user back to same view –  Jun 25 '15 at 09:33
  • (1) whether it is better to **make another view and controller for displaying and redirect to that view** ? (2) is it better to use**Viewbag in same view** ? ,kindly inform valid solution for this purpose –  Jun 25 '15 at 09:33
  • @afsal.akbarsha, Best practice is to follow the PRG pattern and redirect to another view (and its what I would recommend) - but there are many ways to skin a cat :) –  Jun 25 '15 at 10:06

0 Answers0