简体   繁体   English

MVC3模型在布局中为局部

[英]MVC3 Model In Layout As Partial

I'm trying to utilize the MVC3 model validation in my project as of current, however I want to have a simple login section to show in the layout at all times if the user is not logged in. I have the majority of code in place, however the only thing I'm stuck on is how I can post the model back to the form for any validation messages that I produce and need to return. 我正在尝试从当前版本开始在我的项目中利用MVC3模型验证,但是如果用户未登录,我希望在任何时候都在布局中显示一个简单的登录部分。我已经准备好了大部分代码,但是我唯一要坚持的就是如何将模型发布回表单,以获取我产生并需要返回的任何验证消息。

Normally something like this will work: 通常,这样的事情会起作用:

public ActionResult Login()
{
    return View();
}

[HttpPost]

public ActionResult Login(LoginModel)
{
    if(ModelState.IsValid())
    {
        //Run Further checks & functions
        //Upon successful login, retuns to somewhere (Just site index in this example)
        return RedirectToAction("Index", "Site");
    }

    return View(model);
}

Now obviously this won't work as I can't return View(model); 现在显然这将无法工作,因为我无法return View(model); on the partial unless I just want the login form to be displayed, however I want it to post back to the page that I have been editing from. 除非我只希望显示登录表单,否则我希望将其发布回我从其进行编辑的页面。 For example: I navigate to a certain page, contact us, and want to login now. 例如:我导航到某个页面,与我们联系,并希望立即登录。 I enter my details on the form that is always available and I enter my details. 我在始终可用的表单上输入我的详细信息,然后输入我的详细信息。 An error occurs (Incorrect password, incorrect login, account doesn't exist etc...) and I should be brought back to the contact page with the form still filled in with the details that I entered (except obviously password) with validation summary working etc... 发生错误(密码错误,登录错误,帐户不存在等),并且应该带回到我的联系页面,并且该表格仍然填写了我输入的带有验证摘要的详细信息(显然是密码除外)工作中...

Also, any forms on the page that the layout has rendered still need to work correctly (with models etc) 此外,页面上已呈现布局的任何表单仍需要正常工作(使用模型等)

I'm open to suggestions on how to get this working by other means of submission/return however it would be ideal to have the MVC model validation working. 我乐于接受有关如何通过其他提交/返回方式来实现此目的的建议,但是使MVC模型验证有效是理想的。

If anyone needs me to elaborate on anything said, feel free to comment. 如果有人需要我详细说明任何内容,请随时发表评论。 I'll be actively responding for a while. 我会积极响应一段时间。

See this question: How do I pass value to MVC3 master page ( _layout)? 看到以下问题: 如何将值传递给MVC3母版页(_layout)? There are complete guide what to do to pass your model to layout 有完整的指南如何将模型传递到布局

you should create a partial view for login and instead of using "@Html.BeginForm" use @Html.AjaxBegin which submit your page by Ajax call and it RenderHtmlString of login view. 你应该创建一个局部视图,用于登录,而不是使用“@ Html.BeginForm”使用@ Html.AjaxBegin它通过Ajax调用提交您的网页,并登录查看RenderHtmlString。 for eg 例如

public ActionResult Login(LoginModel)
{
    if(ModelState.IsValid())
    {
        //Run Further checks & functions
        //Upon successful login, retuns to somewhere (Just site index in this example)
        return RedirectToAction("Index", "Site");
    }

    return RenderPartialViewToString("Login",model);
}

protected string RenderPartialViewToString(string viewName, object model)
    {
        if (string.IsNullOrEmpty(viewName))
            viewName = ControllerContext.RouteData.GetRequiredString("action");

        ViewData.Model = model;

        using (StringWriter sw = new StringWriter())
        {
            ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
            ViewContext viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
            viewResult.View.Render(viewContext, sw);
            return sw.GetStringBuilder().ToString();
        }
    }

after adding "RenderPartialViewToString" method which will return you " RenderHtmlString " of your partial view. 添加“ RenderPartialViewToString”方法后,该方法将返回部分视图的“ RenderHtmlString ”。 you must be pass viewName and Model as parameter to this Method. 您必须将viewName和Model作为参数传递给此Method。 in your partail View. 在您的partail视图中。

<div id="targetId">
</div>

@using(Ajax.BeginForm("Login",new AjaxOptions{ HttpMethod="POST", UpdateTargetId="targetId"}))
{
  <input type="submit" value="save" />
}

Note: you must be pass UpdateTargetId there your result will Append. 注意:您必须传递UpdateTargetId,结果将追加。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM