I'm still beginning to learn ASP.Net MVC pls help! So I have two controllers with this relation in the Models:
public class QualificationMap
{
public int Id { get; set; }
public virtual ICollection<QualificationBatch> QualificationBatches { get; set; }
}
public class QualificationBatch
{
public int Id { get; set; }
public int QualificationMapId { get; set; }
public virtual QualificationMap QualificationMap { get; set; }
}
Currently I have a generic scaffolded CRUD from these Models, and in the Index of the QualificationMaps View, I'm trying to get the id of one row and routing it to another controller with the following code:
<td nowrap>
@Html.ActionLink("Add Batch", "Create", "QualificationBatches", new { id = item.Id }, null) |
</td>
then I'm trying to use this id from the QualificationMapIndex in the Create action of QualifiationBatchesController with something that may be too straightforward like
public ActionResult Create(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
QualificationMap qm = db.QualificationMaps.Find(id);
if (qm == null)
{
return HttpNotFound();
}
return View(qm);
}
The solution builds fine up until the time where I would click the ActionLink I mentioned above and returns this error as such:
The model item passed into the dictionary is of type 'System.Data.Entity.DynamicProxies.QualificationMap_***, but this dictionary requires a model item of type 'MySolutionName.Models.QualificationBatch'.
You may have already have an understanding where I got it wrong but I have absolutely no idea on what is the correct way of doing this.
Is it possible for some of the input fields in a Create Action be based on what you clicked from a different controller?
EDIT:
My error as have been pointed below is that my Create.cshtml is based on @model QualificationBatch instead of @model QualificationMap. But I have yet to figure out how to do what I initially wanted using ViewModels