简体   繁体   中英

How can i edit multiple SQL Tables in a View in MVC 4 with using Razor engine?

In my SQL, There are two table which I want to edit on my MVC 4 project. Both of them has content like:

id   Title   Description
1    title1  desc1
2    title2  desc2
.    .       .
.    .       .

I tried to get them in a View to edit both table in same time. Model of table1:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace emirhanozkan.Models
{
public partial class PersonInfo
{
    [Key]
    public int id { get; set; }

    public string Title { get; set; }

    public string Description { get; set; }
}
}

Model of table2 has same content only class name is Profile.

Then, set them as list in other class like:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using emirhanozkan.Models;

namespace emirhanozkan.Models
{

    public class AllProfile
    {
        emirhanozkanContext db = new emirhanozkanContext();
        public List<PersonInfo> ListPersonInfo { set; get; }
        public List<Profile> ListProfile { set; get; }
    }
}

My controller is:

public ActionResult EditProfileInfo()
        {
            var nl = new AllProfile();
            nl.ListPersonInfo = db.PersonInfoes.ToList();
            nl.ListProfile = db.Profiles.ToList();
            return View(nl);
        }
        [HttpPost]
        public ActionResult EditProfileInfo(AllProfile allprofile)
        {
            db.Entry(allprofile).State = System.Data.EntityState.Modified;
            foreach (var item in allprofile.ListPersonInfo)
            {
                db.PersonInfoes.Where(x => x.id == item.id);
                db.PersonInfoes.Where(x => x.Title == item.Title);
                db.PersonInfoes.Where(x => x.Description == item.Description);
            }
            foreach (var item in allprofile.ListProfile)
            {
                db.Profiles.Where(x => x.id == item.id);
                db.Profiles.Where(x => x.Title == item.Title);
                db.Profiles.Where(x => x.Description == item.Description);
            }
            db.SaveChanges();
            return RedirectToAction("ProfileInfo");
        }

My View is:

@model emirhanozkan.Models.AllProfile

@{
    ViewBag.Title = "EditProfileInfo";
}

<h2>EditProfileInfo</h2>

@using (Html.BeginForm(null, null, FormMethod.Post)) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <fieldset>

        <legend>PersonInfo</legend>
        @foreach (var item in Model.ListPersonInfo)
        {
            @Html.HiddenFor(model => item.id)
            <div class="editor-field">
                @Html.EditorFor(model => item.Title)
                @Html.ValidationMessageFor(model => item.Title)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => item.Description)
                @Html.ValidationMessageFor(model => item.Description)
            </div>
        }
        @foreach (var item in Model.ListProfile)
        {
            @Html.HiddenFor(model => item.id)
            <div class="editor-field">
                @Html.EditorFor(model => item.Title)
                @Html.ValidationMessageFor(model => item.Title)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => item.Description)
                @Html.ValidationMessageFor(model => item.Description)
            </div>
        }

        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
}

When I press the edit button db.Entry(allprofile).State = System.Data.EntityState.Modified; has an error like:

An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code
Additional information: The entity type AllProfile is not part of the model for the current context.

My you help me to edit these tables in a View?

Agree with markpsmith. My suggestion is to create viewmodel which will have same properties as your entity. In controller map view model with two of your entities then go save to database one by one entity.

As far as I can see there is one error in which you are trying to set state of a model to modified. Note that there is no need to explicitly do that. When you modify your entities, entity do that for you.

Other than that in your post action you are not actually modifying anything. I think this is what you are trying to do.

 [HttpPost] 
public ActionResult EditProfileInfo(AllProfile allprofile) { 
foreach (var item in allprofile.ListPersonInfo) { 
var dbPerson = db.PersonInfoes.FirstOrDefault(x => x.id == item.id);
 dbPerson.Title = item.Title;
    dbPerson.Title = item.Description; } 
        db.SaveChanges();
 return RedirectToAction("ProfileInfo"); 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM