Note that there are some explanatory texts on larger screens.

plurals
  1. POIn Asp.Net MVC view dont render changes in model made by controller
    primarykey
    data
    text
    <p>Im new to ASP.NET MVC and need some help.<br> When "P01" action is fired by post i receive the strongly typed model as parameter, change properties of this model instance and call "return View(model);".<br> In view im using "@Html.TextBoxFor(m => m.p01campo01)" syntax.<br> Someone <a href="https://stackoverflow.com/questions/5410438/mvc3-razor-viewdata">here</a> had a similar problem and get advice to use the syntax im using.<br> Someone <a href="https://stackoverflow.com/questions/642939/mvc-updatemodel-complextype">here</a> is using "&lt;%=Html.TextBox("Person.LastName", ViewData.Model.LastName)%>" syntax.<br> <strong>The problem is</strong> when the view is rendered the textbox have the last posted value, not the value i assigned in controller. </p> <p>Thanks for everyone that have tried to help me here, i put as answer the first answer that work.</p> <p>There is my code: </p> <pre><code>************************************************* using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Globalization; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Security; namespace MyTest.Models { //------- public class testePg01 { [DataType(DataType.Text)] [Display(Name = "p01campo01")] public string p01campo01 { get; set; } [DataType(DataType.Text)] [Display(Name = "p01campo02")] public string p01campo02 { get; set; } } //------- } ************************************************* ************************************************* using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using MyTest.Models; namespace MyTest.Controllers { public class TesteController : Controller { //------- [AcceptVerbs(HttpVerbs.Get)] public ActionResult P01() { return View(); } //------- [AcceptVerbs(HttpVerbs.Post)] [ValidateAntiForgeryToken] public ActionResult P01(testePg01 model) { model.p01campo01 = model.p01campo01 + "#updatedHereButNotInView"; model.p01campo02 = model.p01campo02 + "#updatedHereButNotInView"; return View(model); // it dont return updated } //------- } } ************************************************* ************************************************* @model MyTest.Models.testePg01 @{ ViewBag.Title = "P01"; } &lt;h2&gt;P01&lt;/h2&gt; &lt;script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"&gt;&lt;/script&gt; &lt;script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"&gt;&lt;/script&gt; @Html.ValidationSummary(true, "Erro na pagina.") @using (Html.BeginForm()) { @Html.AntiForgeryToken() &lt;div&gt; &lt;fieldset&gt; &lt;legend&gt;Test P01&lt;/legend&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(m =&gt; m.p01campo01) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.TextBoxFor(m =&gt; m.p01campo01) @Html.ValidationMessageFor(m =&gt; m.p01campo01) &lt;/div&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(m =&gt; m.p01campo02) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.TextBoxFor(m =&gt; m.p01campo02) @Html.ValidationMessageFor(m =&gt; m.p01campo02) &lt;/div&gt; &lt;p&gt; &lt;input type="submit" value="P01" /&gt; &lt;/p&gt; &lt;/fieldset&gt; &lt;/div&gt; } ************************************************* </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload