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
Add a ment  | 

5 Answers 5

Reset to default 2


View: 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