我正在试图找出mvc,所以我有问题...... 我有这个模型:
public class Company
{
public string ID { get; set; }
public string Symbol { get; set; }
public string Description { get; set; }
}
和控制器:
public ActionResult EditCompany()
{
ViewBag.Message = "Edit Company Page";
return View();
}
public ActionResult PartialEditCompany()
{
DataSet dataSet = client.SelectCompanies();
ObservableCollection<Company> inventoryList = new ObservableCollection<Company>();
foreach (DataRow dataRow in dataSet.Tables[0].Rows)
{
inventoryList.Add(new Company
{
ID = (String)dataRow["ID"],
Symbol = (String)dataRow["Symbol"],
Description = (String)dataRow["Description"]
});
}
return PartialView(inventoryList);
}
并查看EditCompany:
@model Test.Models.Company
@{
ViewBag.Title = "EditCompany";
}
<link rel="stylesheet" type="text/css" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css">
<script type="text/javascript" src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<link href="~/Content/mycss.css" rel="stylesheet" />
<script>
$('#loadData').click(function () {
$('#partial').load('/HomeController/PartialEditCompany/');
});
</script>
<div id="stylized2" class="myform">
@using (Html.BeginForm("SaveCompany", "Home", FormMethod.Post, new { @class = "insertcompanyform" }))
{
@Html.ValidationSummary(true)
<table>
<tr>
<td class="first">@Html.Label("ID:")
</td>
<td class="second">@Html.TextBoxFor(m => m.ID)</td>
</tr>
<tr>
<td class="first">
@Html.Label("Symbol:")
</td>
<td class="second">@Html.TextBoxFor(m => m.Symbol)</td>
</tr>
<tr>
<td class="first">
@Html.Label("Description:")
</td>
<td class="second">@Html.TextBoxFor(m => m.Description)</td>
</tr>
<tr>
<td class="third"><input type="button" value="Traži" id="loadData" /></td>
</tr>
<tr>
<td>
<div id="partial"></div>
</td>
</tr>
和finnaly partialview PartialEditCompany:
@using System.Web.Helpers
@{
var grid = new WebGrid(Model, canPage: true, rowsPerPage: 5,
selectionFieldName: "selectedRow", ajaxUpdateContainerId: "gridContent");
grid.Pager(WebGridPagerModes.NextPrevious);
}
<link rel="stylesheet" type="text/css" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css">
<script type="text/javascript" src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<link href="~/Content/mycss.css" rel="stylesheet" />
@Html.WebGrid()
我想要做的是点击按钮(带有id = loadData)来调用将网格放入div标签的部分视图(id = parital)。请帮忙!我做错了什么?
答案 0 :(得分:3)
我在这里评论的是我用来调用部分观点的一些观点
查看强>
<div id="partial" style="display: none">
@{Html.RenderAction("PartialEditCompany"); }
</div>
控制器
[ChildActionOnly]
public ActionResult PartialEditCompany()
{
//stuff you need and then return the partial view
//I recommend using "" quotes for a partial view
return PartialView("inventoryList");
}
然后,如果您想在点击时显示部分视图,则只显示包含div的div。
但是,如果您想使用带参数的局部视图,可以执行以下操作
的Javascript
function getview(parameter){
var url = "@Html.Raw(Url.Action("PartialEditCompany", "Controller", new { parameterID = "-parameter" }))";
url = url.replace("-parameter", parameter);
$('#partial').load(url);
/*Better code by October 11, 2017, you can use this instead of the 3 lines on top*/
/*
var url = "@Html.Raw(Url.Action("PartialEditCompany", "Controller", new { parameterID = "-parameter" }))";
url = url.replace("-parameter", parameter);
$.ajax({
url: url,
type: "GET",
cache: false,
success: function(result){
$("#partial").html(result)
},
error: function(){
//handle your error here
}
});
*/
}
使用javascript,您可以调用局部视图并将参数传递给控制器。当然,您没有指定是否需要参数,但这里是我使用部分视图的解决方案。如果你愿意或者你想要,你可以调用你的javascript函数onclick事件。 (或者在标签上使用data-attr,在click事件中使用javascript或jquery,在不创建的情况下获取data-attr值,这可能是不显眼的方式)
<input type="button" value="Traži" id="loadData" onclick='getview(parameter)' />
希望这对部分观点有所帮助。