javascript asp.net mvc 使用 Java 脚本渲染局部视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22064162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
asp.net mvc Render a Partial View with Java Script
提问by Zapnologica
I want to make a Partial view that displays data in a table.
我想制作一个在表格中显示数据的局部视图。
I will have a Select element with the services to choose from.
我将有一个 Select 元素,其中包含可供选择的服务。
When the user Selects a Service in the combobox I want to the call a partial view with the service Id number:
当用户在组合框中选择服务时,我想使用服务 ID 号调用部分视图:
How can I do this?
我怎样才能做到这一点?
Here is a action method which will render the partialView
这是一个动作方法,它将呈现partialView
//
// GET: /Service/ServiceStatusLogs/1
public ActionResult ServiceStatusLogs(int id)
{
var db = new EFServiceStatusHistoryRepository();
IList<ServiceStatusHistory> logs = db.GetAllStatusLogs(id);
return View("_ServiceStatusLogs", logs);
}
Here is the main action method which returns the page:
这是返回页面的主要操作方法:
//
// GET: /Services/Status
public ActionResult Status()
{
IList<Service> services;
using (var db = new EFServiceRepository())
{
services = db.GetAll();
}
return View(services);
}
采纳答案by ssilas777
You can make use $.ajax functionality to achieve, check this :-
您可以使用 $.ajax 功能来实现,请检查:-
//Combo box change event
$("#comboboxName").change(function () {
//Get service Id
var serviceId = $("#comboboxName").val();
//Do ajax call
$.ajax({
type: 'GET',
url: "@Url.Content("/Service/ServiceStatusLogs/")",
data : {
Id:serviceId //Data need to pass as parameter
},
dataType: 'html', //dataType - html
success:function(result)
{
//Create a Div around the Partial View and fill the result
$('#partialViewContainerDiv').html(result);
}
});
});
Also you should return partial view instead of view
你也应该返回局部视图而不是视图
//
// GET: /Service/ServiceStatusLogs/1
public ActionResult ServiceStatusLogs(int id)
{
var db = new EFServiceStatusHistoryRepository();
IList<ServiceStatusHistory> logs = db.GetAllStatusLogs(id);
return PartialView("_ServiceStatusLogs", logs);
}
回答by Marko
Try this:
试试这个:
public ActionResult ServiceStatusLogs( int id )
{
//Prepare your model
return PartialView( "UserDetails", model );
}
Any then use javascript(ajax) to load contents for an element of the DOM:
Any 然后使用 javascript(ajax) 加载 DOM 元素的内容:
$('#user_content').load('/Service/ServiceStatusLogs');