简体   繁体   English

如何返回更新的局部视图及其父视图(回发)

[英]How to return updated partial View along with its parent View (postback)

I have a Parent View with 2 partial views inside it, the partial views contains few textbox and a button, when I submit the button, the partial views will show some message. 我有一个父视图,其中有2个局部视图,该局部视图包含几个文本框和一个按钮,当我提交按钮时,局部视图将显示一些消息。 Code below : 代码如下:

View : 查看:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage(Of Test.ParentModels)" %>

<% Html.RenderPartial("LogIn", Model.LoginModel)%>
<% Html.RenderPartial("Register", Model.RegisterModel)%>

Model : 型号:

public class LoginModel
{
    //code here
}

public class RegisterModel
{
    //code here
}

public class ParentModel
{
    public LoginModel LoginModel {get; set;}
    public RegisterModel RegisterModel {get; set;}
}

Controller : 控制器:

//LogIn page will call this method on submit
public LogIn(LoginModel model) 
{
    //do something that changed Login html view
    return View("LogIn", model);
}

The problem is I don't know how to return the updated partial view along with its parent (postback) and I don't want to use ajax to achieve this. 问题是我不知道如何返回更新的局部视图及其父视图(回发),并且我不想使用ajax实现此目的。 I tried both return View("LogIn", model) and return PartialView("LogIn", model), both only shows the partial view without its parent. 我尝试了既返回View(“ LogIn”,model)又返回PartialView(“ LogIn”,model),两者都只显示不带父视图的局部视图。 Any help will be appreciated and sorry for bad english. 任何帮助将不胜感激,并为英语不好对不起。

Since you want to do a full page postback, though you postback from the partialview part, in the corresponding Action Method you need to return the Main View(which has two partials in your case) and also capture whatever state required from postback and set it to the model object ( if you need to persist the state) as expected by the Main View. 由于您要进行整页回发,尽管您是从partialview部分回发,所以在相应的操作方法中,您需要返回主视图(在您的情况下有两个部分), 还需要捕获回发所需的任何状态并进行设置到模型对象(如果需要持久保存状态),如主视图所期望的那样。

Eg 例如

return View("ParentViewName",objParentModel);

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

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