简体   繁体   English

如何从ASP.NET MVC中的同一操作返回两个不同的视图?

[英]How do I return two different Views from the same Action in ASP.NET MVC?

I have two views which will both use the same Controller method: 我有两个视图,都将使用相同的Controller方法:

//webServiceController.cs

//The actual method is about 40 lines of code. Truncated for readability.
public ActionResult Index()
{
    object i = new List<WebServiceMethod>();
    i = svcService.populateList("Programs");

    return View(i);

}

The first view is an HTML page that displays the data in a pretty table output: 第一个视图是一个HTML页面,该页面在漂亮的表格输出中显示数据:

<% // Index.aspx %>

<table>
<tbody>
<% foreach (var item in Model) { %>

<tr>
    <td>
        <% if (Convert.ToInt32(item.numberRequests) > 0)
        {%>
            <%= Html.ActionLink("Details", "Details", new { programNumber = item.programNumber })%>
        <%} %>
    </td>
    <td>
        <%= Html.Encode(item.programNumber) %>
    </td>
</tr>

<% } %>
</tbody>
</table>

The second view is a quick'n'dirty conversion to JSON so that I can do magical AJAX tricks with the data: 第二个视图是快速将dirty转换为JSON,以便我可以对数据进行神奇的AJAX技巧:

<% 
    // AjaxGetServiceData.aspx

    // Convert web service response object into JSON for AJAX.
    var jss = new System.Web.Script.Serialization.JavaScriptSerializer();
    Response.Write(jss.Serialize(Model));

%>

I'd created a duplicate of the Index() method and called it AjaxGetServiceData() , but that defeats the purpose of MVC. 我创建了Index()方法的副本,并将其称为AjaxGetServiceData() ,但这违背了MVC的目的。


Resolution: 解析度:

I didn't ask my question very well, as evidenced by a 5-10 minute discussion I just had with a coworker about this very topic. 我并没有很好地提出问题,我刚刚与同事就这个话题进行了5-10分钟的讨论,就证明了这一点。 He kept asking me the same question that many users on this page asked me: "How does the controller know which view to return?" 他一直问我这个页面上许多用户问的相同问题:“控制器如何知道要返回哪个视图?” I responded, "That's what I'm trying to figure out." 我回答说:“这就是我要弄清楚的。” I was trying to get the method to return a different view (or Json output) when AJAX was the requester. 我试图获取当AJAX是请求者时返回不同视图(或Json输出)的方法。 A string argument in the method was my solution. 该方法中的字符串参数是我的解决方案。

This is what I ended up using to get my desired effect: 这是我最终用来获得所需效果的方法:

public ActionResult Index(string isJSON = "no")
{

    /// ...All the code from before...

    if (isJSON == "yes")
    {
        return Json(i, JsonRequestBehavior.AllowGet);
    }
    else
    {
        return View(i);
    }

}

Then, when I want the JSON version, in my AJAX request I specify the URL as /MyController/Index/?isJSON=yes 然后,当我想要JSON版本时,在我的AJAX请求中,将URL指定为/MyController/Index/?isJSON=yes

When I want my pretty table view, I just use /MyController/ 当我想要漂亮的表格视图时,只需使用/MyController/

public ActionResult Index()
{
    object i = new List<WebServiceMethod>();
    i = svcService.populateList("Programs");

    if (someCondition)
        return View(i);
    else
        return View("AjaxGetServiceData", i); // or whatever you called your view.aspx
}

It sounds like you have two different purposes in which case I think you are going the right way when you talk about different controller methods. 听起来您有两个不同的目的,在这种情况下,当您谈论不同的控制器方法时,我认为您的做法正确。

Sure, reuse code inside each controller method but if you want a different result, use a different method and keep the controller methods simple. 当然,可以在每个控制器方法内重用代码,但是如果您想要不同的结果,请使用不同的方法并使控制器方法保持简单。

" ...I was trying to get the method to return a different view (or Json output) when AJAX was the requester..." “ ...我试图获取当AJAX是请求者时返回不同视图(或Json输出)的方法...”

public ActionResult Index()
{
    object i = new List<WebServiceMethod>();       
    i = svcService.populateList("Programs");       

    if (Request.IsAjaxRequest == "True")
    {
        return Json(i, JsonRequestBehavior.AllowGet);           
    }
    else
    {
        return View(i)
    }
}

暂无
暂无

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

相关问题 如何从ASP.NET MVC中的其他操作返回视图 - How to return a view from a different action in ASP.NET MVC 返回ASP.NET MVC中不同视图的同一个控制器 - Return different views same controller in ASP.NET MVC 如何使用 ASP.NET MVC 生成多个包含附加数据的文件并将其返回到 Razor 视图? - How do I generate and return multiple files with additional data to Razor views using ASP.NET MVC? 如何在ASP.NET MVC Controller上使用两个名称共享同一操作 - How can I share same action with two names on ASP.NET MVC Controller 如何从 ASP.NET 5 MVC 6 控制器操作返回 Javascript - How to return Javascript from an ASP.NET 5 MVC 6 controller action asp.net mvc4一个动作的两个differend视图 - asp.net mvc4 two differend views for one action ASP.NET MVC如何将控制器操作指向其他视图? - ASP.NET MVC How do I point a controller action to a different view? 在同一控制器中的两个ActionResult之间传递数据,从而在ASP.NET MVC中呈现不同的视图 - Passing data between two ActionResults in the same controller rendering different views in asp.net mvc 如何从ASP.NET MVC中的同一操作重定向到另一个视图或外部站点? - How can I redirect to either another view or an external site from the same action in ASP.NET MVC? 在MVC ASP.NET中的解决方案中的两个不同应用程序项目的不同视图之间导航 - Navigating between different Views from two different Application Projects within a Solution in MVC ASP.NET
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM