Javascript 重载返回视图的控制器时,如何基于ViewBag属性加载不同的内容?
我有2个Javascript 重载返回视图的控制器时,如何基于ViewBag属性加载不同的内容?,javascript,c#,asp.net,asp.net-mvc,razor,Javascript,C#,Asp.net,Asp.net Mvc,Razor,我有2个索引函数 public ActionResult Index ( ) { ... } 及 第二种方法添加特定对象: ViewBag.orgcatJSON = PD.mapOrgs2Cats(); 到ViewBag,而第一个方法没有。如果我调用了第二个方法,我需要使用Javascript来处理该对象;如果我调用了第一个方法,我不会。所以我现在做的是 var ogmap = @Html.Raw(@ViewBag.orgcatJSON); $(function() { if (o
索引
函数
public ActionResult Index ( )
{
...
}
及
第二种方法添加特定对象:
ViewBag.orgcatJSON = PD.mapOrgs2Cats();
到ViewBag
,而第一个方法没有。如果我调用了第二个方法,我需要使用Javascript来处理该对象;如果我调用了第一个方法,我不会。所以我现在做的是
var ogmap = @Html.Raw(@ViewBag.orgcatJSON);
$(function() {
if (ogmap != undefined)
{
// do something
}
});
但这看起来很糟糕。有更好的方法吗?如果您想根据方法在视图中显示不同的内容,请执行此操作并使用两个视图。然后可以对相关内容使用partials来保存内容 像这样分离视图的一个主要优点是,您已经使依赖项(在本例中是
ViewBag
变量)更加清晰。在您的示例中,您必须一直深入javascript以发现可能需要一些时间的细节。根据经验,我总是尽量保持自己的观点愚蠢(即完成任务所需的逻辑性)
例如:
控制器/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");
}
[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);
}
视图/Example/View1.cshtml:
public ActionResult Index ( )
{
//...
return View("View1");
}
[HttpPost]
public ActionResult Index (HttpPostedFileBase file, string selectedOrgName, string selectedCatName)
{
//...
ViewBag.orgcatJSON = "some json string";
return View("View2");
}
[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);
}
视图1
@Html.Partial(“共享内容”)
视图/Example/View2.cshtml:
public ActionResult Index ( )
{
//...
return View("View1");
}
[HttpPost]
public ActionResult Index (HttpPostedFileBase file, string selectedOrgName, string selectedCatName)
{
//...
ViewBag.orgcatJSON = "some json string";
return View("View2");
}
[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);
}
视图2
@Html.Partial(“共享内容”)
var ogmap=@Html.Raw(ViewBag.orgcatJSON);
$(函数(){
//这里的功能
});
视图/Example/SharedContent.cshtml:
public ActionResult Index ( )
{
//...
return View("View1");
}
[HttpPost]
public ActionResult Index (HttpPostedFileBase file, string selectedOrgName, string selectedCatName)
{
//...
ViewBag.orgcatJSON = "some json string";
return View("View2");
}
[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);
}
你好,世界
为了扩展更清晰的依赖点,可以使用
ModelBinder
绑定预期的类型,从而使依赖点更清晰。这样,您的依赖关系就更加隐蔽,您可以用直接绑定的json替换对ViewBag
的使用
有关什么是ModelBinder
及其工作原理的更多信息,我建议您阅读
如果决定采用此方法,请将第二个索引方法和第二个视图更改为以下内容:
控制器/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");
}
[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);
}
视图/Example/View2.cshtml:
public ActionResult Index ( )
{
//...
return View("View1");
}
[HttpPost]
public ActionResult Index (HttpPostedFileBase file, string selectedOrgName, string selectedCatName)
{
//...
ViewBag.orgcatJSON = "some json string";
return View("View2");
}
[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);
}
@model System.String
视图2
@Html.Partial(“共享内容”)
var ogmap=@Html.Raw(模型);
$(函数(){
//这里的功能
});
为什么不使用不同的视图?你可以使用partials,这样你的代码就不需要了。在我看来,你可能真正想做的是简单地发出一个ajax请求,然后从第二个索引函数返回orgcatJSON?@MarkRucker不能使用ajax,因为这是一种上传文件的方法啊!我现在明白了。好电话。然后我们做一些类似于Carrie在下面建议的事情,只是我们将JavaScript放入了部分脚本中。