Note that there are some explanatory texts on larger screens.

plurals
  1. POCreate ActionResult for save throws error saying The model item passed into the dictionary is of type
    primarykey
    data
    text
    <p>Although this error is very common in the forum, but i am not able to understand how to fix it in my project. I am new to MVC framework. </p> <p>View code:-</p> <pre><code> @model ClassifiedProject.Models.CreateAdvertVM &lt;div class="editor-label"&gt;@Html.LabelFor(model =&gt; model.AdvTitle) &amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;i&gt;(E.g. Old Samsung Galaxy Tab 2)&lt;/i&gt;&lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.AdvTitle) @Html.ValidationMessageFor(model =&gt; model.AdvTitle) &lt;/div&gt; &lt;div class="editor-label"&gt;@Html.LabelFor(model =&gt; model.AdvDescription)&lt;/div&gt; &lt;div class="editor-field"&gt; @Html.TextAreaFor(model =&gt; model.AdvDescription) @Html.ValidationMessageFor(model =&gt; model.AdvDescription) &lt;/div&gt; &lt;div class="editor-label"&gt;@Html.Label("Advertisement Category")&lt;/div&gt; &lt;div class="editor-label"&gt; @Html.DropDownListFor(model =&gt; model.SelectedCategoryId, Model.Categories, new { @class = "ddlcs" }) @Html.ValidationMessageFor(model =&gt; model.SelectedCategoryId) &lt;/div&gt; &lt;p&gt;&lt;input type="submit" value="Save" /&gt;&lt;/p&gt; </code></pre> <p>Controller code of Save button actionresult:- </p> <pre><code> [HttpPost] public ActionResult Create(TR_ADVERTISEMENT tr_advert) { if (ModelState.IsValid) { tr_advert.CreatedDate = tr_advert.ModifiedDate = DateTime.Now; if (tr_advert.IsPriceOnRequest) { tr_advert.CurrencyID = 0; tr_advert.Price = 0; } db.ADVERTISEMENT.Add(tr_advert); db.SaveChanges(); return RedirectToAction("Index"); } </code></pre> <p>Controller code for the form in render stage:-</p> <pre><code> // GET: /Advert/Create public ActionResult Create() { var model = new CreateAdvertVM(); ViewBag.Message = "Post New Advertisement."; ////Render Category DDL var cat = from s in db.CategoryDbSet where s.IsActive == true orderby s.CatName select new { s.CatID, s.CatName }; var catListItems = cat.ToList().Select(c =&gt; new SelectListItem { Text = c.CatName, Value = c.CatID.ToString() }).ToList(); catListItems.Insert(0, new SelectListItem { Text = "[--Select the category--]", Value = "" }); model.Categories = catListItems; return View(model); </code></pre> <p>ViewModel inherited from EF class:- </p> <pre><code> [NotMapped] public class CreateAdvertVM : TR_ADVERTISEMENT { [DisplayName("Category")] [Required] public int? SelectedCategoryId { get; set; } public IEnumerable&lt;SelectListItem&gt; Categories { get; set; } } </code></pre> <p>EF Model:- </p> <pre><code> public class TR_ADVERTISEMENT { [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int AdvID { get; set; } [Required] [DisplayName("Sub Category")] public int SubCatID { get; set; } public int CurrencyID { get; set; } [DisplayName("Price on request")] public bool IsPriceOnRequest { get; set; } [DisplayName("Posted Date")] [DisplayFormat (DataFormatString="{0:dd-MM-yyyy}")] public Nullable&lt;System.DateTime&gt; CreatedDate { get; set; } public Nullable&lt;System.DateTime&gt; ModifiedDate { get; set; } } </code></pre> <p>On the save button click, i have to save the data into the tr_advertisement table using the EF model. </p> <p>Please suggest the solution to this problem.</p> <p><img src="https://i.stack.imgur.com/xkcUV.png" alt="enter image description here"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload