MVC-Html.Action使用Java脚本检索元素,然后将其作为参数传递给Controller,然后返回PartialView [英] MVC - Html.Action to retrieve element using Javascript, then pass it as parameter to Controller, then return a PartialView
问题描述
视图-我的视图具有一个模态,该模态具有一个@Html.Action
,该模式在Controller
中调用PartialViewResult
.请注意,@Html.Action("RetrieveItemPrice", new { item_Id = 1 })
仍具有预定义的item_Id
.
View - My view has a modal that has an @Html.Action
that calls the PartialViewResult
in Controller
. Notice the @Html.Action("RetrieveItemPrice", new { item_Id = 1 })
still has predefined item_Id
.
Q#1:如何创建将获取元素值并将其放入item_Id
参数中的函数,然后再将其发送到Controller
?可以将其插入@Html.Action
吗?
Q#1: How do I create a function that will get an element's value and put it in the item_Id
parameter before sending it to the Controller
? Could this be inserted in the @Html.Action
?
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<div id="pvPrice" class="" style="border: 0px solid green; ">
@Html.Action("RetrieveItemPrice", new { item_Id = 1 })
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<table id="dbTable">
<thead>
<tr>
<th class="hidden">
@Html.DisplayNameFor(model => model.itemId)
</th>
<th>
@Html.DisplayNameFor(model => model.itemName)
</th>
</tr>
</thead>
<tbody id="dbBody">
@foreach (var item in Model.Items)
{
<tr>
<td class="hidden">
@Html.DisplayFor(modelItem => item.itemid)
</td>
<td>
@Html.DisplayFor(modelItem => item.itemname)
</td>
</tr>
}
</tbody>
</table>
控制器
public PartialViewResult RetrieveItemPrice(string item_Id)
{
var prices = from ip in _odb.ITEM_PRICE_MSTR
join i in _odb.ITEM_MSTR on i.ITM_ID equals i.ITM_ID
select new ItemModel
{
itemid = i.ITM_ID,
itemname = i.ITM_DESC,
itemprice = ip.ITM_PRICE,
defaultflag = ip.DEFAULT_FL,
startdate = DbFunctions.TruncateTime(ip.START_DT),
enddate = DbFunctions.TruncateTime(ip.END_DT),
createddt = i.CREA_DT
};
prices = prices.Where(i => i.itemid.ToLower().Contains(item_Id.ToLower()));
prices = prices.OrderByDescending(i => i.itemprice);
var vm = new ItemViewModel();
vm.Items = prices.ToPagedList(pageNumber, pageSize);
return PartialView("_ViewItemPrice", vm);
}
Q#2:当我使用PartialViewResult
在Controller
中返回PartialView
时,谁会接收并将其显示在View
中?我运行了代码,但未显示任何内容.我想我仍然缺少View
中的代码来接收Controller
Q#2: When I return a PartialView
in Controller
using PartialViewResult
, who's going to receive and display it in the View
? I ran the code, but nothing is being displayed. I think I still lack codes in View
to receive the returned PartialView
in Controller
推荐答案
A#1:调用@Html.Action
的代码在服务器端执行,在该阶段html文档尚未发送到客户端,因此您不必寻找一种检索元素值的方法.您可以直接将其传递给调用,因为用于渲染视图(包括元素)的模型应该可以被模式中的代码访问.
A#1: the code calling @Html.Action
is executed on server-side, at that phase the html document has not been sent to client-side so you don't have to look for a way to retrieve element value. You can pass it directly to the call, instead, because the model used for rendering the view (including the element) should be accessible to the code in the modal.
如果将模式标记直接放置在视图内部,则可以肯定获得例如@ Model.ItemId. 如果您将模态放置在局部视图中,则可以通过ViewBag传递商品ID:
If you place the modal markups right inside the view, you definitely can get @Model.ItemId, for example. If you place the modal in a partial view, you can pass item id via ViewBag:
@Html.Partial("Modal", null, new ViewDataDictionary{{"ItemId", Model.ItemId}})
然后在模式标记中使用它:
then use it in the modal markups:
@Html.Action("RetrieveItemPrice", new { item_Id = ViewBag.ItemId })
A#2:如果[ChildActionOnly]
使操作RetrieveItemPrice
起作用,则应尝试操作.
A#2: you should try if [ChildActionOnly]
makes the action RetrieveItemPrice
works.
这篇关于MVC-Html.Action使用Java脚本检索元素,然后将其作为参数传递给Controller,然后返回PartialView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!