[英]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.