Reputation: 185
I am learning .net MVC by working on a mini project in which I am calling a view which shows details of a model passed to it. Now I also have some buttons ( to edit , delete etc) that model . I wanted to know if I can pass the model directly to an action in its controller through ActionLink so that the action can call another view to edit the details of that model. Here is the view I am working with::
@model MvcBooksList.Models.Book
@{
ViewData["Title"] = "Book Details";
}
@if (Model != null)
{
<div>
<div>
@Html.DisplayNameFor(model => model.BookName)
@Html.DisplayFor(model => model.BookName)
</div>
<div>
@Html.DisplayNameFor(model => model.Author)
@Html.DisplayFor(model => model.Author)
</div>
<div>
@Html.DisplayNameFor(model => model.Publisher)
@Html.DisplayFor(model => model.Publisher)
</div>
<div>
@Html.DisplayNameFor(model => model.Price)
@Html.DisplayFor(model => model.Price)
</div>
<div>
@Html.DisplayNameFor(model => model.Pages)
@Html.DisplayFor(model => model.Pages)
</div>
<div>
@Html.DisplayNameFor(model => model.Category)
@Html.DisplayFor(model => model.Category)
</div>
<div>
@Html.DisplayNameFor(model => model.Subcategory)
@Html.DisplayFor(model => model.Subcategory)
</div>
<div>
@Html.DisplayNameFor(model => model.AddedBy)
@Html.DisplayFor(model => model.AddedBy)
</div>
<div>
@Html.DisplayNameFor(model => model.ModifiedBy)
@Html.DisplayFor(model => model.ModifiedBy)
</div>
<div>
@Html.DisplayNameFor(model => model.AddedOn)
@Html.DisplayFor(model => model.AddedOn)
</div>
<div>
@Html.DisplayNameFor(model => model.LastModifiedOn)
@Html.DisplayFor(model => model.LastModifiedOn)
</div>
<div>
@Html.ActionLink("Edit", "Edit", }) | // CAN I PASS MODEL HERE WHICH IS PASSED
@Html.ActionLink("Delete", "Delete", }) | //TO THIS VIEW
@Html.ActionLink("Delist", "Delist", }) |
@Html.ActionLink("Cancel", "Cancel")
</div>
</div>
}
else
{
<h3>No Book with that Name</h3>
}
Upvotes: 0
Views: 880
Reputation: 765
You don't need to pass complete model to the edit or delete view , it's an overkill. Just pass id of the book to those views by sending route values in action link.
@Html.ActionLink("Edit","Edit",new{id=bookid})
Here bookid is the id of the book. Please remember to use id as a parameter in your Edit action method.
public ActionResult Edit(int id)
Upvotes: 1