user5583390
user5583390

Reputation:

Pass Data to Controller from Table in View with C#/Razor MVC

I have a main view with multiple partial views in my C# MVC. The initial partial view contains a list which is populated from a model in a table. I want to be able to click a row on the table, and then populate another partial view based on the ID.

I have initially used: <tr type="submit" onclick = "location.href = '@(Url.Action("RowClicked", "Test", new { id = @Html.DisplayFor(modelItem => item.ID) }))'" > which did pass the ID to the server, but it took me to a new page.

I am not sure if I need to use forms. I want to be able to click one of the rows to redraw another partial view based on the value of the ID in the row clicked without the whole page refreshing.

How do you send data using Razor/C# to the server in general without calling a new page, and how do I do this from each row of a table?

Thanks

Upvotes: 0

Views: 2295

Answers (1)

HEGDE
HEGDE

Reputation: 511

Few hints you can follow :

Option 1: Inherit from parent page

By default, any partial view rendered by calling @Html.Partial("PartialViewName") will get the view model passed to the parent view.

So if you have:

View Model

namespace MyNamesapce
{
    public OrderInfoViewModel
    {
        public string OrderTitle { get; set; }
        public IEnumerable<OrderItem> OrderItems { get; set; }
    }
}

OrderInfo.cshtml

@model MyNamespace.OrderInfoViewModel

<h1>@Model.OrderTitle</h1>

@Html.Partial("OrderLineItems")

The OrderLineItems page should get a MyNamespace.OrderViewModel passed to it... so your partial view should look like this:

OrderLineItems.cshtml

@model MyNamespace.OrderInfoViewModel

foreach (var orderItem in Model.OrderItems)
{
    //Do stuff
}

Option 2: Specify model

You can use the second parameter to specify the view model to be passed. I.e.

OrderInfo.cshtml

@model MyNamespace.OrderInfoViewModel

<h1>@Model.OrderTitle</h1>

@Html.Partial("OrderLineItems", Model.OrderItems)

OrderLineItems.cshtml

@model IEnumerable<OrderItem>

foreach (var orderItem in Model)
{
    //Do stuff
}

Option 3: Use partial actions

If you need to reuse a partial view over multiple pages, it could be a good idea to use a partial view to eliminate having to populate different view models with the same info just because the page is going to be using the same partial.

E.g.

View Model

namespace MyNamesapce
{
    public OrderInfoViewModel
    {
        public string OrderTitle { get; set; }
    }
}

Controller

public class OrderController : Controller
{
    public ActionResult OrderInfo(int orderId)
    {
        OrderInfoViewModel viewModel = GetViewModel(orderId);
        return View(viewModel);
    }

    public PartialViewResult OrderLineItems(int orderId)
    {
        IEnumerable<OrderItem> orderItems = GetOrderItems(orderId);
        return Partial(orderItems);
    }
}

OrderInfo.cshtml

@model MyNamespace.OrderInfoViewModel

<h1>@Model.OrderTitle</h1>

@Html.Action("OrderLineItems")

OrderLineItems.cshtml

@model IEnumerable<OrderItem>

foreach (var orderItem in Model.OrderItems)
{
    //Do stuff
}

Upvotes: 3

Related Questions