[英]Populating nested list object of model based on checkbox selections in ASP.NET MVC
我對ASP.NET MVC還是比較陌生,發現自己有點糊塗,或者有點像湯。 這是長度說明,請耐心等待。 很抱歉,這似乎有點長而無聊:)。 所以,這是我的問題:
這是我的模型。
public class SocialAccount
{
public int SocialAccountID { get; set; }
public int UserID { get; set; }
public int SocialSiteID { get; set; }
public string SocialAccountUsername { get; set; }
public string SocialAccountUserID { get; set; }
public string OAuthToken { get; set; }
public string Captcha { get; set; }
public string UserCaptchaValue { get; set; }
}
public class SocialSitePost
{
public int PostID { get; set; }
public string PostContent { get; set; }
public string PostAttachments { get; set; }
public List<SocialSite> SelectedSocialSites { get; set; }
public List<SocialAccount> SocialAccount { get; set; }
}
public class SocialSite : Audit
{
public int SocialSiteID { get; set; }
public string SocialSiteName { get; set; }
}
成功驗證用戶身份后,我會將一些用戶詳細信息存儲在會話中,然后重定向到主頁。 這是該代碼:
//action method in some Controller
public Login()
{
//Authenticate user
//Get all social account related information
SomeRepository someRepository = new SomeRepository();
List<SocialAccount> socialAccountDetails = someRepository.SomeMethod(user.UserID);
//Add social account related information to session
HttpHelper.StoreInSession("UserDetails", socialAccountDetails);
//redirect to HomePage
return RedirectToAction("HomePage", "Account");
}
在HomePage操作方法中,我從會話中檢索用戶對象並檢查已注冊的社交帳戶。 我還創建了SocialSite
的列表對象,以維護所有已注冊社交網站的列表。 然后,將其打包到ViewData
並將其發送到視圖。 這是該代碼:
//action method in some other controller
public ActionResult HomePage()
{
List<SocialSite> allSocialSites = null;
List<SocialAccount> userSocialAccountDetails = HttpHelper.RetrieveFromSession("UserSocialSiteDetails") as List<SocialAccount>;
if (userSocialAccountDetails != null && userSocialAccountDetails.Count != 0)
{
allSocialSites = new List<SocialSite>();
bool hasFacebookAccount = userSocialAccountDetails.Exists(x => x.SocialSiteID == Convert.ToInt32(CommonConstants.SocialSite.Facebook));
if (hasFacebookAccount)
{
allSocialSites.Add(new SocialSite() { SocialSiteID = 1, SocialSiteName = "Facebook" });
}
bool hasTwitterAccount = userSocialAccountDetails.Exists(x => x.SocialSiteID == Convert.ToInt32(CommonConstants.SocialSite.Twitter));
if (hasTwitterAccount)
{
allSocialSites.Add(new SocialSite() { SocialSiteID = 2, SocialSiteName = "Twitter" });
}
}
ViewData["SocialSites"] = allSocialSites;
return View();
}
我沒有強類型的觀點。 我需要做的是發布用戶回復。 因此,我創建了一個POST表單。 現在在此表單中,我想顯示所有已注冊社交帳戶的復選框(ViewData會給我此信息)。 因此,可以說,如果用戶A僅注冊了一個Facebook帳戶,則僅應顯示一個代表Facebook的復選框。 如果用戶B注冊了一個Facebook,Twitter和Instagram帳戶,則應顯示3個復選框,其中每個代表一個社交帳戶。 我想你明白了。 這是我的代碼:
@model SocialSitePost
@{
using (Html.BeginForm("ProcesssRequest", "Account", FormMethod.Post))
{
<div class="divProcessRequest">
<br />
<div>
@Html.TextAreaFor(model => model.PostContent)
</div>
<div>
@foreach (var socialSite in ViewData["SocialSites"] as List<SocialSite>)
{
@Html.CheckBox("SocialSiteID", new { value = socialSite.SocialSiteID, @checked = true });
@Html.Label(socialSite.SocialSiteName)
} //Tried this..And am able to display the checkbox
@*@for (int i = 0; i < Model.SelectedSocialSites.Count; i++)
{
@Html.CheckBoxFor("SocialSiteID", new { value = Model.SelectedSocialSites[i].SocialSiteID, @checked = true });
}*@ //Tried this too..I know this shouldn't work and rightly so, it doesn't work :)
</div>
<br />
<div>
<input type="submit" id="btnPost" value="Post" />
</div>
</div>
}
}
說夠了!!! 現在我真正的問題是 :
當用戶選中任何復選框時,我要填充SocialSitePost
對象的SelectedSocialSites
屬性。 這是表單要回發其對象的模型。 我需要能夠在POST方法中訪問SelectedSocialSites
屬性。 這就是我的POST方法:
[HttpPost]
public ActionResult ProcessRequest(SocialSitePost post)
{
if (ModelState.IsValid)
{
List<SocialSite> allSocialSites = ViewData["SocialSites"] as List<SocialSite>; //Can't get anything here
int socialSiteNo = post.SelectedSocialSites.SocialSiteID; //This is what I want to be able to do
return View("HomePage");
}
}
由於我對ASP.NET MVC還是比較陌生,因此我不確定這是否是正確的方法。 我確實嘗試了一些類似EditorFor的方法,嘗試在第一次呈現視圖時發送SocialSitePost
對象(我不想這樣做,因為它沒有任何邏輯意義)。
有人可以告訴我如何根據用戶對復選框的選擇在POST方法中獲取SocialSitePost
類的SelectedSocialSites
屬性嗎?
另外,有人可以告訴我我在這里做錯什么了嗎,因為到目前為止我在SO上還沒有發現任何類似於這種情況的問題?
這里的主要問題是該模型不完全適合需要顯示的內容。 也就是說,關於視圖的模型格式及其在后端的表示形式是不同的。 因此,這里需要使用ViewModel。 這就是解決我的問題的方法。
這些是我最終設計的ViewModel:
public class SocialSiteViewModel : SocialSite
{
public bool IsSelected { get; set; }
public string SocialSitePostID { get; set; }
}
public class SocialSitePostViewModel : SocialSitePost
{
public List<SocialSiteViewModel> SocialSiteViewModel { get; set; }
}
然后,我可以像這樣輕松地在視圖上使用它們:
using (Html.BeginForm("ActionMethodName", "ControllerName", FormMethod.Post))
{
@Html.CheckBoxFor(x => x.SocialSiteViewModel[i].IsSelected)
@Html.HiddenFor(x => x.SocialSiteViewModel[i].SocialSiteID)
@Html.HiddenFor(x => x.SocialSiteViewModel[i].SocialSiteName)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.