Mam prosty formularz edycji dla modelu użytkownika, ale kiedy wysyłam z powrotem, żadna z ukrytych wartości wejściowych nie są stosowane do modelu i nie jestem pewien, dlaczego tak się dzieje.Post MVC nie dodając wartości do modelu
My Razor:
@model CMS.Core.Models.UserProfile
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset class="normalForm">
<legend>User Profile</legend>
@Html.HiddenFor(model => model.UserId)
<div class="formRow">
<div class="editor-label">
@Html.LabelFor(model => model.EmailAddress)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.EmailAddress, new { @class = "textbox" })
@Html.ValidationMessageFor(model => model.EmailAddress)
</div>
</div>
<div class="formRow">
<div class="editor-label">
@Html.LabelFor(model => model.FirstName)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.FirstName, new { @class = "textbox" })
@Html.ValidationMessageFor(model => model.FirstName)
</div>
</div>
<div class="buttonRow"><input type="submit" value="Save" class="button" /></div>
</fieldset>
}
mojego kontrolera: class
[HttpPost]
public ActionResult Edit(UserProfile user)
{
if (ModelState.IsValid)
{
user.Save();
return RedirectToAction("Index");
}
return View(user);
}
UserProfile:
[Table("UserProfile")]
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; private set; }
[Required(ErrorMessage = "Please enter an email address")]
[StringLength(350)]
[DataType(DataType.EmailAddress)]
[Display(Name = "Email Address")]
public string EmailAddress { get; set; }
[StringLength(100)]
[DataType(DataType.Text)]
[Display(Name = "First Name")]
public string FirstName { get; set; }
}
Gdy próbuję user.UserId
powraca do zera (tak jak jest to int), ale jeśli Próbuję Request["UserId"]
zwraca poprawną wartość, więc wartość jest wysyłana poprawnie - po prostu nie dodano do modelu UserProfile
. Czy ktoś wie dlaczego tak się dzieje i co mogę zrobić, aby rozwiązać go
Dzięki
Jak jest generowany HTML do ukrytego pola? Czy możesz też opublikować swój model UserProfile? – nemesv
– Pete
Wygenerowany HTML wygląda dobrze. Czy możesz zaktualizować swój wpis definicją klasy 'UserProfile'? – nemesv