【发布时间】:2018-03-02 02:29:02
【问题描述】:
我是 MVC 的新手。如何在下面的代码中组合两个操作结果:
public ActionResult TreeView()
{
var db = new PCNMSContext();
return View(db.AssetRegisters.Where(x => !x.ParentAssetID.HasValue).ToList());
}
public ActionResult AssetRegisterIndex(string SearchString)
{
var assetregisters = from m in db.AssetRegisters
select m;
if (!String.IsNullOrEmpty(SearchString))
{
assetregisters = assetregisters.Where(s => s.AssetNumber.Contains(SearchString));
}
return View(assetregisters);
}
上面的ActionResult是..第一,在TreeView上生成列表,第二是用Search函数在Table上生成列表。
我的模型:
public class AssetRegister
{
public int AssetID {get;set;}
public string AssetNumber {get;set}
public string AssetDescription {get;set;}
public int? ParentAssetID {get;set;}
[ForeignKey("ParentAssetID")]
public virtual AssetRegister Parent {get;set;}
public virtual ICollection<AssetRegister> Childs {get;set;}
}
我的观点:
<div class="row">
<div class="col-lg-4">
<div id="jstree">
@(Html.TreeView(Model)
.EmptyContent("root")
.Children(m => m.Childs)
.HtmlAttributes(new { id = "tree" })
.ChildrenHtmlAttributes(new { @class = "subItem" })
.ItemText(m => m.AssetNumber)
.ItemTemplate(
@<text>
<a href="@item.AssetID" desc="@item.AssetID">@item.AssetNumber</a>
</text>)
)
</div>
</div>
@using (Html.BeginForm())
{
<div class="col-lg-1">
@Html.TextBox("SearchString")
<button class="btn" type="submit" value="Search">View</button>
</div>
}
<div class="col-lg-7">
<table> I didnt copy all, but this table contains a column of 'AssetRegister' model. There are 4 columns. AssetID,ParentID, AssetNumber,AssetDescription
</table>
</div>
</div>
我的 jsTree 脚本:
<script>
$(function () {
var selectedData;
$('#jstree').jstree({
"core": {
"multiple": true,
"check_callback": true,
'themes': {
"responsive": true,
'variant': 'small',
'stripes': false,
'dots': true
}
},
"types": {
"default": {
"icon": "glyphicon glyphicon-record"
},
"root": {
"icon": "glyphicon glyphicon-ok"
}
},
"plugins": ["dnd", "state", "types", "sort"]
}).on('changed.jstree', function (e, data) {
var i, j, r = [];
for (i = 0, j = data.selected.length; i < j; i++) {
r.push(data.instance.get_node(data.selected[i]).text);
}
$('#SearchString').val(r.join(', '));
});
</script>
所以当我点击节点时,节点的文字会出现在@Html.TextBox上。
问题是,在表中加载数据和在树视图上加载数据是不同的。
1. 我怎样才能结合那些ActionResult?
2.当文本框接收到来自节点的文本时,提交按钮不应该被点击,而是应该被自动点击。我该怎么做?
谢谢。
【问题讨论】:
-
这都是为了同一个页面吗?
-
是的,在同一个视图上...
-
您想如何调用
AssetRegisterIndex()方法 - 它有一个参数,所以该参数基于您的TreeView视图中的某些内容? -
@StephenMuecke,正确...所以树视图上的文本将是第二个 actionresult 的搜索参数。
-
如果您最初想要所有
AssetRegister,请在TreeView视图中使用@Html.Action(..)来显示初始结果。要根据文本框过滤结果,您需要 ajax 调用AssetRegisterIndex方法并更新页面(但该方法需要返回Partialview,而不是View)
标签: c# asp.net-mvc asp.net-mvc-5