javascript 将参数从视图传递到控制器剃刀
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9412362/
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
pass parameter from view to controller razor
提问by Laziale
I would like to pass parameter from view to controller in cshtml page. This is the code I have:
我想将参数从视图传递到 cshtml 页面中的控制器。这是我的代码:
<script type="text/javascript">
function Test() {
$.get(
"@Url.Action("UpdateReport/", "Report")"
);
}
</script>
<h2>Reports</h2>
@foreach (var item in Model)
{
<div>@Html.ActionLink(@item.Name, "ActionCall", "Report", new { Id = @item.Id }, null )</div><br /><br />
<input type="button" class="button1" value="Insert" />
<input type="button" class="button1" value="Delete" />
<input type="button" onclick="Test()" class="button1" value="Update" />
}
When I click the update button I would like to pass the @item.Id to the controller action via the JS code I have. The code is stepping in to the controller action now, but without parameters. I want to pass a parameter as well. Thanks in advance for every advice, Laziale
当我单击更新按钮时,我想通过我拥有的 JS 代码将 @item.Id 传递给控制器操作。代码现在进入控制器操作,但没有参数。我也想传递一个参数。提前感谢您的每一个建议,Laziale
public ActionResult UpdateReport(string name)
{
return View("Index");
}
采纳答案by Jayanga
you can use $.post
你可以使用 $.post
<script type="text/javascript">
function Test() {
var itemId = $('#MyButton').attr('itemid');
var url = '@Url.Action("UpdateReport", "Outcome")';
var data = { Id:itemId };
$.post(url, data, function (result) {
var id = '#postedFor' + postId;
$(id).html(result);
});
);
}
</script>
and bind the item id to your input
并将项目 ID 绑定到您的输入
<input id="MyButton" itemid="@Html.Raw("postedFor" + Model.Id)" type="button" onclick="Test()" class="button1" value="Update" />
回答by jim tollan
<script type="text/javascript">
function Test() {
$.get(
"@Url.Action("UpdateReport/", "Report")",
{Id : @item.Id}
);
}
</script>
or:
或者:
<script type="text/javascript">
function Test() {
$.get(
"@Url.Action("UpdateReport/", "Report", new {Id = item.Id})"
);
}
</script>
回答by Murat Y?ld?z
View:In the View "category"parameter is passed to the Controller with "admin"value
视图:在视图中“category”参数以“admin”值传递给Controller
@{ Html.RenderAction("Menu", "Nav", new { category = "admin" }); }
Controller:In the Controller "category"parameter's value is obtained.
控制器:在控制器“类别”参数的值中获得。
public PartialViewResult Menu(string category, string menu = null)
{
IEnumerable<Menu> menus = repository.Menus
.Select(x => x)
.Where(x => (x.MenuCategory == category)
.OrderBy(x => x.MenuSequence).ToList();
return PartialView(menus);
}
回答by Chris Tesene
Add a new routevalue item, named Id
添加一个新的路由值项,名为 Id
<script type="text/javascript">
function Test() {
$.get(
"@Url.Action("Update", "Report", new { @id = item.Id })"
);
}
回答by Azargoth
Try this code in your javascript function:
在你的 javascript 函数中试试这个代码:
$.get('Report/UpdateReport?paramName=paramValue', function(data) {
//Do something with "data"
});
Where paramName is name of the parameter in controller's action, value is current value you wanto to pass. Data returned by controller is placed in variable "data".
其中 paramName 是控制器动作中参数的名称,value 是您想要传递的当前值。控制器返回的数据放在变量“data”中。