I am currently getting a error in the model saying it cannot convert from String to a Model.
This happens once I try and add a new database table entry. I am using SQL Server and for the front end I am using c# mvc3 razor view.
Model:
namespace NBProfiler.Model
{
using System;
using System.Collections.Generic;
public partial class PersonContempancy
{
public int Id { get; set; }
public int PersonId { get; set; }
public Nullable<System.DateTime> AttainedDate { get; set; }
public int FrameworkId { get; set; }
public int ContempancyCategoryId { get; set; }
public int ContempancyId { get; set; }
public int ContempancyLevelId { get; set; }
public int FrameworkLevelId { get; set; }
public virtual Contempancy Contempancy { get; set; }
public virtual ContempancyCategory ContempancyCategory { get; set; }
public virtual FrameworkLevel FrameworkLevel { get; set; }
public virtual Framework Framework { get; set; }
public virtual Person Person { get; set; }
public virtual ContempancyLevel ContempancyLevel { get; set; }
}
}
Controller:
public ActionResult Create()
{
ViewBag.Contepancies = db.Contempancies.ToList();
ViewBag.ContempancyCategory = db.ContempancyCategories.ToList();
ViewBag.ContempancyLevel = db.ContempancyLevels.ToList();
ViewBag.FrameworkLevel = db.FrameworkLevels.ToList() ;
ViewBag.Person = db.People.ToList();
ViewBag.Framework = db.Frameworks.ToList();
return View();
}
//
// POST: /Mapping/Create
[HttpPost]
public ActionResult Create(PersonContempancy personcontempancy)
{
if (ModelState.IsValid)
{
db.PersonContempancies.Add(personcontempancy);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.Contepancies = db.Contempancies.ToList();
ViewBag.ContempancyCategory = db.ContempancyCategories.ToList();
ViewBag.ContempancyLevel = db.ContempancyLevels.ToList();
ViewBag.FrameworkLevel = db.FrameworkLevels.ToList();
ViewBag.Person = db.People.ToList();
ViewBag.Framework = db.Frameworks.ToList();
return View(personcontempancy);
}
And finally View: (I have removed the other items from the view as they are all the same code so to shorten it on here I deleted it)
@model NBProfiler.Model.PersonContempancy
@{
ViewBag.Title = "Create";
}
<h2>Map Skill to Person</h2>
<script src="../../Scripts/jquery-1.5.1.min.js" type="text/javascript"></script>
<link href="../../Content/themes/start/jquery-ui-1.8.22.custom.css" rel="stylesheet"
type="text/css" />
<script src="../../Scripts/jquery-ui-1.8.11.min.js" type="text/javascript"></script>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>PersonContempancy</legend>
<div class="editor-label">
Name:
</div>
<div class="editor-field">
Html.DropDownListFor(m => m.Person, new SelectList(ViewBag.Person as System.Collections.IEnumerable, "PersonId", "PersonName"), "Choose Name")
</div>
@*<div class="editor-field">*@
<div class="editor-label">
@*@Html.LabelFor(m => m.ContempancyCategory)*@
</div>
<div class="editor-field">
@Html.DropDownListFor(m => m.ContempancyCategory, new SelectList(ViewBag.ContempancyCategory as System.Collections.IEnumerable, "ContempancyCategoryId", "ContempancyCategoryName"),"Select", new {id = "category" })
<p>
<input name="submit" type="submit" value="Map Skill" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Show Table", "Index")
</div>
<script type="text/javascript">
$(document).ready(function () {
$("#category").change(function () {
var idCat = $(this).val();
$.getJSON("/Mapping/contempancies", { CategoryID: idCat },
function (MyData) {
var select = $("#contempancy");
select.empty();
$.each(MyData,function(index, itemData) {
select.append($('<option/>', {
value: itemData.Value,
text: itemData.Text
}));
});
});
});
});
</script>
Thanks!
Looks like the problem is your treating your view model as an entity model which is never a good idea.
When passing data to/from a view you should have an explicit view model and only pass the data that your view needs. Also, you are coupling your data layer with your view which defeats the purpose of MVC!
In your example, if
PersonContempancyis indeed a view model (and not an entity) then you should use it to create a new entity mapping the data it needs, AutoMapper is a pretty nifty library for that sort of thing.