Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8356859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:18:25+00:00 2026-06-09T10:18:25+00:00

I am currently getting a error in the model saying it cannot convert from

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T10:18:26+00:00Added an answer on June 9, 2026 at 10:18 am

    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 PersonContempancy is 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.

    public class PersonContempancyViewModel
    {
        // update with properties the view needs
    }
    
    [HttpPost] 
    public ActionResult Create(PersonContempancyViewModel viewModel) 
    { 
        if (ModelState.IsValid) 
        {
            var entity = new PersonContempancyEntity();
            // map properties from view model to entity
            // entity.FrameworkId = viewModel.FrameworkId etc..
            db.PersonContempancies.Add(entity);  
            db.SaveChanges(); 
    
            return RedirectToAction("Index");   
        }
    
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently getting this error: System.Data.SqlClient.SqlException: New transaction is not allowed because there
I'm doing a Firefox addon but I'm currently getting this error in the Error
I'm using prepared statements and MySQLi, and am currently getting this error. PHP Catchable
i keep getting this error, The current value 'String.Empty' type is not compatible with
I am currently getting the following error on my post model which is under
I'm currently getting a Could not load type UI.Administration.Site.master error message and I'm not
I'm currently getting the user's birthday from Facebook and storing it in the standard
I am currently getting first day Of this week and last week values with
I am currently getting images from the 'Documents' directory (using -imageWithContentsOfFile:) with no problems
I can't see why in my application i am getting this error. As all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.