简体   繁体   English

当重载一个返回View的Controller时,我该如何根据ViewBag属性加载不同的内容?

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

相关问题 如何将ViewBag从控制器传递到View(Ajax Url) - How can I pass a ViewBag from controller to View(Ajax Url) 如何在控制器中将ViewBag值包含到javascript中 - How to include ViewBag value into javascript in view of controller 如何在ASP.NET MVC 5中将我的viewbag内容(一个巨大的表)从视图传递到控制器 - how to pass my viewbag content (a huge table) from view to controller, in ASP.NET MVC 5 我应该在视图中引用controller.model还是controller.content? - Should I refer to controller.model or controller.content in a view? 在Ember中,每当属性更改时,如何在控制器中调用函数 - In Ember How Should I Call a Function in the Controller Whenever A Property Changes 如果在移动设备上查看,应该如何首先加载移动内容? - How Should I load my mobile content first if viewed on mobile? 如何在不同的下拉菜单选择中加载不同的内容? - How do I load different content on different dropdown selection? 我应该如何基于url中的参数加载js函数? - How should I load a js-function based on parameter in url? 如何在无提示的情况下离开页面浏览时将用户输入内容保存在视图中以及对控制器进行后期调用 - How do I save user input content in the view and do a post call to the controller when navigate away from page, silently 我应该将事件绑定放在控制器或视图中吗? - Should I put events binding in controller or view?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM