0

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

Lwrnc Crz
  • 43
  • 1
  • 10
  • You have not shown your `Create.cshtml` view, but clearly it has `@model QualificationBatch` when it needs to be `@model QualificationMap` (and as a side note, do not generate the `SelectList` by calling the db until its needed (and it wont work correctly anyway - refer [Can the ViewBag name be the same as the Model property name in a DropDownList?](https://stackoverflow.com/questions/37161202/can-the-viewbag-name-be-the-same-as-the-model-property-name-in-a-dropdownlist)) –  Jan 30 '18 at 11:42
  • Or you need to return a `QualificationBatch` , not a `QualificationMap` from your controller (but then having a dropdownlist would not make sense so its not really clear what your creating) –  Jan 30 '18 at 11:44
  • your side note makes a lot of sense and I now have edited out the Viewbag that generates a SelectList because I want the QualificationMapId to be a set value based on the ActionLink, I have tried returning a QualificationBatch but it will return nulls since it has no records yet, and I want to start creating records for QualificationBatch using the QualificationMapId and some properties in the QualificationMaps model, but still using the QualificationBatch model as a template in the Create.cshtml because it has a lot of properties that doesn't exist in the first one? – Lwrnc Crz Jan 30 '18 at 11:53
  • If your wanting to create a new `QualificationBatch` for the `QualificationMap` in your first view,, then you need to initialize a new `QualificationBatch` and set its `QualificationMapId` to the value of the `id` parameter and return that to the view (although you will want to use a view model which will also include a property for at least the name of the `QualificationMap` - I assume you omitted some properties of `QualificationMap` in your question) –  Jan 30 '18 at 11:59
  • for example `QualificationBatch model = new QualificationBatchVM{ QualificationMapId = id, QualificationMapName = qm.Name }; return View(model);` –  Jan 30 '18 at 12:00
  • Yes you are correct in your assumptions including my intention to create a new QualificationBatch record after getting an id from a view in QualificationMap. So to achieve this I have to implement a ViewModel? I would first try to read up on that topic – Lwrnc Crz Jan 30 '18 at 12:10
  • Your editing data so you should also use a view model - refer [What is ViewModel in MVC?](https://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc) –  Jan 30 '18 at 12:13

0 Answers0