Вероятно, здесь нет ничего очевидного, но моя модель, отправленная в контроллер, всегда равна null ... Модель правильно переходит к представлению, и я могу получить доступ к свойствам, но при отправке формы модель нулевой. Зачем?MVC модель бритвы post
модель ...
public class ProfileModel
{
public MembershipUser user { get; set; }
public ProfileSettingsModel settings { get; set; }
}
контроллер ...
[HttpPost]
public ActionResult SaveSettings(ProfileModel model)
{
var x = model.user.UserName;
return View();
}
... Посмотреть
@model MyApp.Models.Profile.ProfileModel
@using(Html.BeginForm("SaveSettings","Profile"))
{
//Tried removing these as I shouldn't need them, but no luck.
@Html.HiddenFor(x=>x.settings)
@Html.HiddenFor(x=>x.user)
@Html.CheckBoxFor(model=>model.settings.VisitorsCanAddMeAsFriend)
@Html.LabelFor(x=>x.settings.VisitorsCanAddMeAsFriend, "Visitors can add me as a friend")
@Html.CheckBoxFor(x=>x.settings.VisitorsCanMessageMe)
@Html.LabelFor(x=>x.settings.VisitorsCanMessageMe, "Visitors can message me")
<button id="save" type="submit" class="btn btn-default">Save</button>
}
Вынесено HTML ...
<form action="/Profile/SaveSettings?HttpMethod=POST&InsertionMode=Replace&LoadingElementDuration=0&AllowCache=False" method="post" novalidate="novalidate">
<input data-val="true" data-val-required="The VisitorsCanAddMeAsFriend field is required." id="settings_VisitorsCanAddMeAsFriend" name="settings.VisitorsCanAddMeAsFriend" type="checkbox" value="true">
<input name="settings.VisitorsCanAddMeAsFriend" type="hidden" value="false">
<label for="settings_VisitorsCanAddMeAsFriend">Visitors can add me as a friend</label>
<input data-val="true" data-val-required="The VisitorsCanMessageMe field is required." id="settings_VisitorsCanMessageMe" name="settings.VisitorsCanMessageMe" type="checkbox" value="true">
<input name="settings.VisitorsCanMessageMe" type="hidden" value="false">
<label for="settings_VisitorsCanMessageMe">Visitors can message me</label>
<button id="save" type="submit" class="btn btn-default">Save</button>
</form>
Есть ли что-то очевидное?
Просьба указать ваш ** визуализированный ** HTML. – Dai
Вам нужно вернуть свой профиль ProfileModel. –
'@using (Html.BeginForm (« SaveSettings »,« Profile »))' не отображает html, который вы указали (для тега 'form'). Показать реальный html. –