[英]When overloading a Controller that returns a View, how should I load different content based on a ViewBag property?
I have 2 Index
functions, 我有2个Index
函数,
public ActionResult Index ( )
{
...
}
and 和
[HttpPost]
public ActionResult Index (HttpPostedFileBase file, string selectedOrgName, string selectedCatName)
{
...
}
The second method adds a particular object: 第二种方法添加一个特定的对象:
ViewBag.orgcatJSON = PD.mapOrgs2Cats();
to the ViewBag
, while the first method doesn't. 到ViewBag
,而第一种方法没有。 If I've called the second method, I need to use Javascript to do something with that object; 如果我调用了第二种方法,我需要使用Javascript对该对象执行某些操作; if I've called the first method, I don't. 如果我打电话给第一种方法,我就不会。 So what I'm doing is 所以我正在做的是
var ogmap = @Html.Raw(@ViewBag.orgcatJSON);
$(function() {
if (ogmap != undefined)
{
// do something
}
});
but that seems like very bad form. 但这似乎是非常糟糕的形式。 Is there a better way of doing this? 有没有更好的方法呢?
If you want to have different content in your view depending on the method, then do just that and use two views. 如果您希望在视图中使用不同的内容,具体取决于方法,那么请执行此操作并使用两个视图。 You can then use partials for the related content to keep things DRY . 然后,您可以使用谐音的相关内容,以使事情干 。
A major advantage to separating the views like this is that you've made your dependencies (a ViewBag
variable in this case) much clearer. 分离这样的视图的一个主要优点是你已经使你的依赖(在这种情况下是一个ViewBag
变量)更加清晰。 In your example, you would have to dig all the way into the javascript to spot that detail which could take some time. 在您的示例中,您必须一直深入javascript以发现可能需要一些时间的详细信息。 As a rule of thumb, I always try to keep my views as stupid as possible (ie as little logic as necessary to complete the task). 根据经验,我总是尽量保持我的观点尽可能愚蠢(即完成任务所需的逻辑很少)。
For example: 例如:
Controllers/ExampleController.cs : Controllers / ExampleController.cs :
public ActionResult Index ( )
{
//...
return View("View1");
}
[HttpPost]
public ActionResult Index (HttpPostedFileBase file, string selectedOrgName, string selectedCatName)
{
//...
ViewBag.orgcatJSON = "some json string";
return View("View2");
}
Views/Example/View1.cshtml : 意见/示例/ View1.cshtml :
<h1>View 1</h1>
<!-- specific content here -->
<!-- now include shared content -->
@Html.Partial("SharedContent")
Views/Example/View2.cshtml : 意见/示例/ View2.cshtml :
<h1>View 2</h1>
<!-- specific content here -->
<!-- now include shared content -->
@Html.Partial("SharedContent")
<script>
var ogmap = @Html.Raw(ViewBag.orgcatJSON);
$(function() {
//functionality here
});
</script>
Views/Example/SharedContent.cshtml : Views / Example / SharedContent.cshtml :
<p>Hello World!</p>
To extend the clearer dependencies point, you could make it even clearer by binding your expected type using the ModelBinder
. 要扩展更清晰的依赖关系点,您可以通过使用ModelBinder
绑定您的预期类型来使其更清晰。 Then your dependency would be even less hidden and you could replace your usage of the ViewBag
with your directly bound json. 然后你的依赖关系会更少隐藏,你可以用直接绑定的json替换你对ViewBag
使用。
For more information about what the ModelBinder
is and how it works, I'd suggest you read this post . 有关ModelBinder
是什么以及它如何工作的更多信息,我建议你阅读这篇文章 。
If you decide to go this route, change the second Index method and the second View to the following: 如果您决定使用此路线,请将第二个Index方法和第二个View更改为以下内容:
Controllers/ExampleController.cs : Controllers / ExampleController.cs :
[HttpPost]
public ActionResult Index (HttpPostedFileBase file, string selectedOrgName, string selectedCatName)
{
//...
//let's pass the json directly to the View and make the dependency 100% clear
var json = "some json string";
return View("View2", json);
}
Views/Example/View2.cshtml : 意见/示例/ View2.cshtml :
@model System.String
<!-- in the above line, we are telling the view what type we expect to be bound by the controller -->
<h1>View 2</h1>
<!-- specific content here -->
<!-- now include shared content -->
@Html.Partial("SharedContent")
<script>
var ogmap = @Html.Raw(model);
$(function() {
//functionality here
});
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.