I have following in database (and I use them as models):
Category
{
long id;
string name;
long subcategoryId;
}
Subcategory
{
long id;
string subName;
//other data
}
I get data from db with Entity SQL like:
public static Category GetCategory(long catId)
{
Category cat;
using (Entities db = new Entities())
{
cat = (from c in db.Categories
where c.id == catId
select c).SingleOrDefault();
cat.SubcategoryReference.Load();
}
return cat;
}
Now, I have some partial view:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<Test.Database.Subcategory>>" %>
<%--Some code--%>
<%:Html.ActionLink("Move to new category", "editcategory", "category",
new { model = new Category { Subcategory = item } }, null)%>
Plan is to navigate with this action link to CategoryController and action
public ActionResult EditCategory(Category model)
{
//some code
return View(model);
}
where I can edit some info about this category which would contain chosen subcategory. Problem is that I keep getting model=null as parameter in this EditCategory Action. What am I doing wrong? Any advice is welcome.
You cannot pass complex objects using GET as action links. If you wanted to do this you will need to send all properties, one by one and leave the default model binder cope with it but this could be very tedious:
I would recommend you the standard way to achieve which consists in passing the category id that you are willing to edit:
and inside the controller action fetch the corresponding model from the repository:
Then the corresponding view will contain a form with input fields for all the different category properties and it is the POST action which you will submit this form to that can take a Category model as action parameter: