admin管理员组文章数量:1357302
I would like to pass parameter from view to controller in cshtml page. This is the code I have:
<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
public ActionResult UpdateReport(string name)
{
return View("Index");
}
I would like to pass parameter from view to controller in cshtml page. This is the code I have:
<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
public ActionResult UpdateReport(string name)
{
return View("Index");
}
Share
Improve this question
edited Feb 23, 2012 at 12:15
Laziale
asked Feb 23, 2012 at 11:43
LazialeLaziale
8,23548 gold badges155 silver badges271 bronze badges
5 Answers
Reset to default 2View: In the View "category" parameter is passed to the Controller with "admin" value
@{ 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);
}
<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>
you can use $.post
<script type="text/javascript">
function Test() {
var itemId = $('#MyButton').attr('itemid');
var url = '@Url.Action("UpdateReport", "Oute")';
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
<input id="MyButton" itemid="@Html.Raw("postedFor" + Model.Id)" type="button" onclick="Test()" class="button1" value="Update" />
Add a new routevalue item, named Id
<script type="text/javascript">
function Test() {
$.get(
"@Url.Action("Update", "Report", new { @id = item.Id })"
);
}
Try this code in your javascript function:
$.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".
本文标签: javascriptpass parameter from view to controller razorStack Overflow
版权声明:本文标题:javascript - pass parameter from view to controller razor - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744075879a2586737.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论