我相信這有一個簡單的答案。原諒我的無知;經過15年的ASPNET WebForms,這個新穎的web v2.0的東西正在踢我的後方。MVC狀態 - 對象爲空
我想了解爲什麼我的WebUser
對象在HttpPost上爲null。
所以我有這樣的控制器操作:
[HttpGet]
public ViewResult PrivateKey()
{
var pk = new PrivateKey { WebUser = GetWebUserFromSession() };
return View(pk);
}
這工作得很好,我的Razor視圖可以訪問PrivateKey.WebUser
成員沒有問題。
在HttpPost採取行動的問題出現了:
[HttpPost]
public ActionResult PrivateKey(PrivateKey pkey)
{
if (string.IsNullOrEmpty(pkey.Value))
return View(pkey);
// pkey.WebUser is always null :(
// TestKeyAgainstMockCipher(pkey.Value, pkey.WebUser);
InsertSecuredMasterKeyIntoSession(pkey.Value);
return RedirectToAction("Index", "Home");
}
所以對,我敢肯定的理由是完全合法的,我PrivateKey
對象來通過HttpPost不錯,但包含的PrivateKey
對象中的引用WebUser
財產始終爲空。
我不知道,如果MVC需要[Serializable]
屬性被放置在WebUser
財產或如果我使用HttpPost不正確,或者即使它在MVC剛剛接受實踐不斷跑回I/O數據存儲到不斷地獲取數據,而不是在內存中維護標準的面向對象的數據結構。
如果將有所幫助,這裏就是我試圖讓工作的看法:
@model KeePassWeb.Domain.Models.PrivateKey
@{
ViewBag.Title = "PrivateKey";
Layout = "~/Views/Shared/_MasterPage.cshtml";
}
@using (Html.BeginForm())
{
<div class="privatekey">
<table style="">
<tr>
<td style="width:128px; height: 75px;"></td>
<td style="width:291px; height: 75px;"></td>
</tr>
<tr>
<td style="width:128px; height: 25px;"></td>
<td style="width:291px; height: 25px; ">
@Html.PasswordFor(x =>x.Value, new { @class = "form-control", id="txtMasterPass", style = "width:65%", placeHolder = "Password" })
<input Id="btnMasterPass" type="submit" value="Submit" class="submit" />
</td>
</tr>
</table>
</div>
<table>
<tr>
<td><img src="../Images/big_info.png" height="32px" width="32px" style="margin-right:8px;" alt="info" /></td>
<td>
@if (string.IsNullOrEmpty(@Model.WebUser.Padding)) {
@:This is your first login. Please create a private key passphrase.
}
else {
@:Please enter your private key to access your wallet.
}
</td>
</tr>
</table>
}
<script type="text/javascript">
$("#wallet").addClass("active");
</script>
總之,我很困惑,爲什麼httpPost
控制器操作是從回發收到有效PrivateKey
對象,但包含在PrivateKey
對象中的對象始終爲空。
提前致謝! --Oldtimer :)
你需要顯示你的視圖 - 你如何生成'WebUser'屬性的表單控件? –
add'@ Html.HiddenFor(x => x.WebUser。屬性)'在BeginForm中爲WebUser中的所有屬性設置,以便for在內部具有這些值,並且可以將它們發回給控制器。現在,您的表單對此WebUser沒有任何線索,因此您將得到空值 –
您不會爲「WebUser」的屬性生成任何表單控件,因此不會將與它有關的任何內容提交回控制器。由於您沒有編輯任何與WebUser相關的任何內容,只需在POST方法中重新獲取它(就像您在GET方法中一樣) - 沒有必要通過添加隱藏的輸入來生成額外的html,然後再次將其重新發回(你只會打開自己的惡意用戶過度發佈攻擊) –