Laziale
Laziale

Reputation: 8225

pass parameter from view to controller razor

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");
        }

Upvotes: 0

Views: 21426

Answers (5)

Murat Yıldız
Murat Yıldız

Reputation: 12060


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);
}


Upvotes: 2

Jayanga
Jayanga

Reputation: 887

you can use $.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

<input id="MyButton" itemid="@Html.Raw("postedFor" + Model.Id)" type="button" onclick="Test()" class="button1" value="Update" />

Upvotes: 0

Azargoth
Azargoth

Reputation: 395

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

Upvotes: 0

Chris Tesene
Chris Tesene

Reputation: 21

Add a new routevalue item, named Id

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

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

Upvotes: 0

jim tollan
jim tollan

Reputation: 22485

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

Upvotes: 1

Related Questions