-1

I´m new in MVC, and I´m working in a simple FORM, but,I have a problem, for some reason I want to change the bound model in the controller operation and render the view, but this not happen.

For example:

public class Product{
    int id {get; set;}
    string description {get; set;}
}

and the controller method:

[POST]
public ActionResult Edite(Product p){
    p.description = "HELLOOOOOO!!!"
    return View(P);
}

the view:

@model WebSite.Models.Product

@{
    ViewBag.Title = "Modificar (Product)";
}

<h2>Modificar (Product)</h2>


@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4></h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        @Html.HiddenFor(model => model.Id)

        <div class="form-group">
            @Html.LabelFor(model => model.description, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.description, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.description, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Guardar" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Volver a la Lista", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

but in the rendered view, the description of the product is not "HELLOOOOO!!!!".

Why MVC render the view with the user entered values and not with the new model I want?

  • You'll have to show us the `Edite` view. – John Saunders Mar 09 '15 at 23:00
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Mar 09 '15 at 23:01
  • Your posting a model to `Edite()`. If you want to return a new model, then follow the PRG pattern and redirect to the GET method and build a new model. MVC binds to `ModelState` values when you post and return the view (not to model properties). –  Mar 09 '15 at 23:03
  • Thanks John, i'm sorry, i'm new in Stackoverflow. The posted code is an example, not my real case, but, I'll add the view example. – Cristian Bauza Mar 09 '15 at 23:07
  • @StephenMuecke Thanks for the answer, yes, i was thinking in this option, but, in my real case i want to render a partial view, and y want to do this with ajax without reload the page, it´s posible use PRG in this case? – Cristian Bauza Mar 09 '15 at 23:15
  • No, because ajax stays on the same page and any `return RedirectToAction()` would be ignored. You need to show the script so we can understand what your trying to so. –  Mar 09 '15 at 23:20
  • @StephenMuecke thanks a lot, with your previous answer i searched in google "MVC binds to ModelState values when you post and return the view (not to model properties)" and i find the answer to my problem in this post: http://stackoverflow.com/questions/1775170/asp-net-mvc-modelstate-clear – Cristian Bauza Mar 09 '15 at 23:53
  • The last part of [this answer](http://stackoverflow.com/questions/26654862/textboxfor-displaying-initial-value-not-the-value-updated-from-code/26664111#26664111) explains the reason why this is the default behavior - and its not a bug :) –  Mar 09 '15 at 23:56

1 Answers1

0

maybe isn't the best way to resolve the problem, but this post solved my problem: Asp.net MVC ModelState.Clear

Community
  • 1
  • 1