I am new to ASP.NET MVC and need some help.
When the action "P01" is triggered by post, I get a strongly typed model as a parameter, change the properties of the instance of this model and call "return View (model)".
The im view uses the syntax "@ Html.TextBoxFor (m => m.p01campo01)". Someone here had a similar problem and got advice on using im using syntax.
Someone here uses the syntax "<% = Html.TextBox (" Person.LastName ", ViewData.Model.LastName) → The problem is that when rendering a view, the text field has the last published value, and not the value assigned in the controller .
Thanks to everyone who tried to help me here, I will answer the first answer that works.
There is my 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);
}
}
}
*************************************************
*************************************************
@model MyTest.Models.testePg01
@{
ViewBag.Title = "P01";
}
<h2>P01</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@Html.ValidationSummary(true, "Erro na pagina.")
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
<div>
<fieldset>
<legend>Test P01</legend>
<div class="editor-label">
@Html.LabelFor(m => m.p01campo01)
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.p01campo01)
@Html.ValidationMessageFor(m => m.p01campo01)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.p01campo02)
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.p01campo02)
@Html.ValidationMessageFor(m => m.p01campo02)
</div>
<p>
<input type="submit" value="P01" />
</p>
</fieldset>
</div>
}
*************************************************
source
share