简体   繁体   English

将参数从视图传递到控制器剃刀

[英]pass parameter from view to controller razor

I would like to pass parameter from view to controller in cshtml page. 我想将参数从视图传递到cshtml页面中的控制器。 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. 当我单击更新按钮时,我想通过我拥有的JS代码将@ item.Id传递给控制器​​操作。 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 预先感谢您的每一个建议,Laziale

public ActionResult UpdateReport(string name)
        {
            return View("Index");
        }


View: In the View "category" parameter is passed to the Controller with "admin" value 视图:在视图中,将“类别”参数与“ admin”值一起传递给控制器

@{ 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>

Add a new routevalue item, named Id 添加一个名为id的新routevalue项

<script type="text/javascript">
function Test() {

    $.get(        
    "@Url.Action("Update", "Report", new { @id = item.Id })"                 
);
}

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. 其中paramName是控制器操作中参数的名称,value是您要传递的当前值。 Data returned by controller is placed in variable "data". 控制器返回的数据放置在变量“数据”中。

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" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM