我正在尝试找到最佳解决方案,以便在任何给定页面上我始终拥有登录模型,注册模型和其他模型(或更多)。登录和注册模型分别位于每页的导航栏和页脚中。
具体来说,我有一个情况,我有一个课程页面,根据通过输入的网址查找过的课程从表格填充。因此页面加载了课程页面模型。在页眉和页脚的同一页面上,我需要有登录和注册表单,这些表单都需要自己的模型。
使用foreach循环填充课程页面:
课程页面视图(缩短):
@model IEnumerable<oltinternational_mvc.Models.Course_page>
@{
Layout = "~/Views/Shared/_courselayout.cshtml";
foreach (var cpage in Model) { ViewBag.Title = @cpage.page_title; }
}
@foreach (var cpage in Model)
{
if (cpage.template_type == 2)
{
<div id="main_container">
<article class="content">
<h1>
@cpage.Title
</h1>
<section>
@Html.Raw(cpage.main_image)
<h3>
@cpage.Country
</h3>
<p>@cpage.intro_1</p>
@if (!string.IsNullOrEmpty(cpage.intro_2))
{
<p>@cpage.intro_2</p>
}
@if (!string.IsNullOrEmpty(cpage.intro_3))
{
<p>@cpage.intro_3</p>
}
<a href="javascript:void(0);" id="open-popup" class="button_view_sample_pages">View sample pages</a>
@if (!string.IsNullOrEmpty(cpage.website_button))
{
@Html.Raw(cpage.website_button)
}
else {
<a href="/Home/licensing_options" class="button_licensing_options">Licensing options</a>
}
@Html.Raw(cpage.popup_script)
<div class="clearfloat"></div>
</section>
</article>
</div>
控制器如下:
public ActionResult Course_page(string id)
{
string abbrev = id;
var cpage = from c in db.Course_page
select c;
if (!String.IsNullOrEmpty(abbrev))
{
cpage = cpage.Where(x => x.abbrev.Contains(abbrev));
}
return View(cpage);
}
和模型:
[Table("course_page")]
public class Course_page
{
[Key]
public int CourseID { get; set; }
public string Meta { get; set; }
public string Title { get; set; }
public string Country { get; set; }
public string main_image { get; set; }
public string list_1 { get; set; }
public string list_2 { get; set; }
public string list_3 { get; set; }
public string list_4 { get; set; }
public string list_5 { get; set; }
public string list_6 { get; set; }
public string list_7 { get; set; }
public string list_8 { get; set; }
public string list_9 { get; set; }
public string list_10 { get; set; }
public string testim_1 { get; set; }
public string testim_2 { get; set; }
public string testim_3 { get; set; }
public string course_site { get; set; }
public string popup_script { get; set; }
public string abbrev { get; set; }
public string page_title { get; set; }
public int template_type { get; set; }
public string intro_1 { get; set; }
public string intro_2 { get; set; }
public string intro_3 { get; set; }
public string website_button { get; set; }
}
在导航栏中,我有以下列表项,它引用我的ajax登录形式:
<li class="login">
<a href="javascript:void(0);" tabindex="5"><span>Login</span></a>
@using (Ajax.BeginForm("login", "Account", new AjaxOptions
{
HttpMethod = "POST",
UpdateTargetId = "login_box"
}, new { id = "login_box" }))
{
@Html.Partial("_login")
}
</li>
加载此表单:
@model myproject_mvc.Models.LoginViewModel
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
<div id="login_box">
<div class="sign_up_box"> <a href="#sign_me_up" class="login_button open-popup-signup">Sign up in seconds</a> </div>
@Html.ValidationSummary(true)
@Html.AntiForgeryToken()
<div class="login_box_lower">
<p class="login_box_or">or</p>
<p class="login_sign_in">Sign in</p>
<div style="position:relative;">
@Html.EditorFor(model => model.Email, new { htmlAttributes = new { placeholder = "Email", maxlength = "18", @class = "login_username clearable" } })
<span class="login_username_icon"></span>
</div>
<div style="position:relative;">
@Html.EditorFor(model => model.Password, new { htmlAttributes = new { placeholder = "Password", maxlength = "18", @class = "login_pw clearable" } })
<span class="login_pw_icon"></span>
</div>
<a href="/Account" class="login_button">Login</a>
<div class="clearfloat"></div>
<a href="#" class="login_forgot_pw">Forgot password?</a>
</div>
</div>
使用以下模型:
public class LoginViewModel
{
[Required]
[Display(Name = "Email")]
[EmailAddress]
public string Email { get; set; }
[Required]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[Display(Name = "Remember me?")]
public bool RememberMe { get; set; }
}
页脚中的Register表单也是如此,它使用RegisterViewModel。请记住上面登录的实际表格尚未完成,它现在只是指向/帐户网址。
我尝试了一些我在网上找到的解决方案,但似乎无法使其中的任何一个工作,我认为部分是因为我使用foreach来填充课程页面中的视图模型。我不知道如何使用viewmodel,因为我在页面上使用了foreach循环。
我想知道的是处理这种情况的最佳方法是什么?
答案 0 :(得分:0)
您应该创建并使用viewmodel,而不是直接引用您的对象。也许这样的事情。
创建一个新类MyViewModel.cs(或您喜欢的任何名称)
public class MyViewModel
{
public IEnumerable<oltinternational_mvc.Models.Course_page> CoursePages { get; set; }
public myproject_mvc.Models.LoginViewModel { get; set; }
}
现在在您看来,您将引用视图模型,而不是直接引用模型。像@model myproject_mvc.ViewModels.MyViewModel
在你的foreach循环中你会像这样@foreach (var cpage in Model.CoursePages)