0

Tell me why I get such an error when using the db.Test.Add (view) method; Error: CS1503 Argument 1: cannot convert from 'DC.Models.View' to 'DC.Models.SaveComments.Test'. I try to write data to the database, code:

My Controller:

namespace DC.Controllers
{
    public class CommentController : Controller
    {
        public ActionResult SaveRecord(View model)
        {
            try
            {
                DataComment db = new DataComment();
                View view = new View();
                view.Id = model.Id;
                view.Name = model.Name;

                db.Test.Add(view);

                db.SaveChanges();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return RedirectToAction("Index");
        }
    }
}

Models:

    namespace DC.Models.SaveComments
    {
        public class DataComment : DbContext
        {
            public DataComment()
                : base ("CC")
            { }
            public DbSet<Test> Test { get; set; }
        }
    }

namespace DC.Models.SaveComments
{
    public class Test
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
}

namespace DC.Models
{
    public class View
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
}

And in html-page:

<body>
    @using (Html.BeginForm("SaveRecord", "Comment", FormMethod.Post))
    {
    <p>Id @Html.TextBoxFor(model => model.Id)</p>
    <p>Name @Html.TextBoxFor(model => model.Name)</p>
    <input type="submit" value="Submit" class="btn-block btn-primary" />
    }
</body>

1 Answer 1

1

You made the object of View It should be of test:

namespace DC.Controllers
{
    public class CommentController : Controller
    {
        public ActionResult SaveRecord(View model)
        {
            try
            {
                DataComment db = new DataComment();
                Test view= new Test(); //This is change
                view.Id = model.Id;
                view.Name = model.Name;

                db.Test.Add(view);

                db.SaveChanges();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return RedirectToAction("Index");
        }
    }
}

One more thing their is difference in model namespace please look at that also

Or you can use like below:

public ActionResult SaveRecord(Test model)
        {
            try
            {
                DataComment db = new DataComment();

                db.Test.Add(model);

                db.SaveChanges();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return RedirectToAction("Index");
        }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.