我在Asp.net MVC4应用程序中添加了一个管理区域,我认为自己是一个入门级开发人员。我的编辑[HttpGet]动作正常,因为它返回资产的值。我在网上倾注了一堆MVC4特价,其中大多数使用以下[HttpPost]编辑方法:
[HttpPost]
public ActionResult Edit(ITTESI.AssetTracker.Web.UI.ViewModels.AssetDetailsViewModel models)
{
try
{
if (ModelState.IsValid)
{
_entities.Entry(models).State = EntityState.Modified;
_entities.SaveChanges();
return RedirectToAction("Index", new { ID = models.ID });
}
}
catch (DataException)
{
ModelState.AddModelError("", "Unable able to save changes....epic fail!!!");
}
return View(models);
}
问题是我无权使用以下 .Entry :
_entities.Entry(models).State = EntityState.Modified;
我显然缺少一个可以让我使用它的参考,但我不确定它是什么。
这是我的 ViewModel (我会使用DbContext,但他们没有):
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using ITTESI.AssetTracker.Web.UI.Content.Classes;
namespace ITTESI.AssetTracker.Web.UI.ViewModels
{
public class AssetDetailsViewModel
{
public int ID { get; set; }
public string AssetIdentifier { get; set; }
public string ManufacturerName { get; set; }
public string Model { get; set; }
public string SchoolLocation { get; set; }
public string Status { get; set; }
public string Condition { get; set; }
// [DataType(DataType.MultilineText)]
public string Notes { get; set; }
public Utils.AssignReturn AssignReturnEligible { get; set; }
public string SchoolLocationCd { get; set; }
public string SchoolLocationDisplayValue { get; set; }
public AssignedUserViewModel AssignedUserViewModelObj { get; set; }
}
}
这是我的管理控制器:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Metadata.Edm;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using ITTESI.AssetTracker.Data.EntityModel.Entity;
using ITTESI.AssetTracker.Data.EntityModel.Definition;
using System.Data.Entity;
using ITTESI.AssetTracker.Web.UI;
using ITTESI.AssetTracker.Web.UI.Content;
using ITTESI.AssetTracker.Web.UI.Content.Classes;
using ITTESI.AssetTracker.Web.UI.ViewModelBuilders;
using ITTESI.AssetTracker.Web.UI.ViewModels;
namespace ITTESI.AssetTracker.Web.UI.Controllers
{
public class AdminController : Controller
{
ExtendedITTESI_AssetTrackerEntities _entities = new ExtendedITTESI_AssetTrackerEntities();
public ActionResult Index(ViewModels.AssetDetailsViewModel assetDetails)
{
ViewBag.PageTitle = "Admin Search";
ViewBag.HideShowLocation = "hide";
ViewBag.SubmitButtonValue = "Search";
return View("Index", assetDetails);
}
[HttpGet]
public ActionResult Edit(ITTESI_AssetTracker_Asset asset) // 'ITTESI.AssetTracker.Web.UI.ViewModels.AssetDetailsViewModel'
{
ViewBag.PageTitle = "Edit Asset";
ViewBag.SubmitButtonValue = "Save";
ViewBag.ShowLocation = true;
var model = _entities.ITTESI_AssetTracker_Asset.FirstOrDefault();
return View(model);
}
[HttpPost]
//public ActionResult Edit(ExtendedITTESI_AssetTrackerEntities ate)
public ActionResult Edit(ITTESI.AssetTracker.Web.UI.ViewModels.AssetDetailsViewModel models)
{
try
{
if (ModelState.IsValid)
{
_entities.Entry(models).State = EntityState.Modified;
_entities.SaveChanges();
return RedirectToAction("Index", new { ID = models.ID });
}
}
catch (DataException)
{
ModelState.AddModelError("", "Unable able to save changes....epic fail!!!");
}
return View(models);
}
}
}
这是我的修改视图:
@model ITTESI.AssetTracker.Data.EntityModel.Entity.ITTESI_AssetTracker_Asset
@{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>ITTESI_AssetTracker_Asset</legend>
@Html.HiddenFor(model => model.ID)
<div class="editor-label">
@Html.LabelFor(model => model.AssetIdentifier)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.AssetIdentifier)
@Html.ValidationMessageFor(model => model.AssetIdentifier)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.AssetConditionID)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.AssetConditionID)
@Html.ValidationMessageFor(model => model.AssetConditionID)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.SchoolID)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.SchoolID)
@Html.ValidationMessageFor(model => model.SchoolID)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.AssetCategoryID)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.AssetCategoryID)
@Html.ValidationMessageFor(model => model.AssetCategoryID)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.VendorID)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.VendorID)
@Html.ValidationMessageFor(model => model.VendorID)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.AssignedPersonID)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.AssignedPersonID)
@Html.ValidationMessageFor(model => model.AssignedPersonID)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.AssetStatusID)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.AssetStatusID)
@Html.ValidationMessageFor(model => model.AssetStatusID)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ManufacturerID)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ManufacturerID)
@Html.ValidationMessageFor(model => model.ManufacturerID)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ModelDetail)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ModelDetail)
@Html.ValidationMessageFor(model => model.ModelDetail)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.CreatedOn)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.CreatedOn)
@Html.ValidationMessageFor(model => model.CreatedOn)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.CreatedByIdentifier)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.CreatedByIdentifier)
@Html.ValidationMessageFor(model => model.CreatedByIdentifier)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ModifiedOn)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ModifiedOn)
@Html.ValidationMessageFor(model => model.ModifiedOn)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ModifiedByIdentifier)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ModifiedByIdentifier)
@Html.ValidationMessageFor(model => model.ModifiedByIdentifier)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Notes)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Notes)
@Html.ValidationMessageFor(model => model.Notes)
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
我也确定问题是,对于ExtendedITTESI_AssetTrackerEntities的DbContext不存在,并且在研究之后还有一点是System.Data.Entity的一部分。我对 ExtendedITTESI_AssetTrackerEntities 的唯一代码是:
using System;
using Common.Logging;
using EFCachingProvider;
using EFCachingProvider.Caching;
using EFProviderWrapperToolkit;
using EFTracingProvider;
using ITTESI.AssetTracker.Data.EntityModel.Entity;
namespace ITTESI.AssetTracker.Data.EntityModel.Definition
{
public class ExtendedITTESI_AssetTrackerEntities : ITTESI_AssetTrackerEntities
{
private ILog logOutput;
public ExtendedITTESI_AssetTrackerEntities()
: this("name=ITTESI_AssetTrackerEntities")
{
}
public ExtendedITTESI_AssetTrackerEntities(string connectionString)
: base(EntityConnectionWrapperUtils.CreateEntityConnectionWithWrappers(
connectionString,
"EFTracingProvider",
"EFCachingProvider"
))
{
CachingPolicy = AssetTrackerEntitiesCachingPolicy.CachingPolicy();
Cache = AssetTrackerEntitiesCache.Cache();
}
#region Tracing Extensions
private EFTracingConnection TracingConnection
{
get { return this.UnwrapConnection<EFTracingConnection>(); }
}
public event EventHandler<CommandExecutionEventArgs> CommandExecuting
{
add { this.TracingConnection.CommandExecuting += value; }
remove { this.TracingConnection.CommandExecuting -= value; }
}
public event EventHandler<CommandExecutionEventArgs> CommandFinished
{
add { this.TracingConnection.CommandFinished += value; }
remove { this.TracingConnection.CommandFinished -= value; }
}
public event EventHandler<CommandExecutionEventArgs> CommandFailed
{
add { this.TracingConnection.CommandFailed += value; }
remove { this.TracingConnection.CommandFailed -= value; }
}
private void AppendToLog(object sender, CommandExecutionEventArgs e)
{
if (this.logOutput != null)
{
this.logOutput.Debug(e.ToTraceString().TrimEnd());
}
}
public ILog Log
{
get { return this.logOutput; }
set
{
if ((this.logOutput != null) != (value != null))
{
if (value == null)
{
CommandExecuting -= AppendToLog;
}
else
{
CommandExecuting += AppendToLog;
}
}
this.logOutput = value;
}
}
#endregion
#region Caching Extensions
private EFCachingConnection CachingConnection
{
get { return this.UnwrapConnection<EFCachingConnection>(); }
}
public ICache Cache
{
get { return CachingConnection.Cache; }
set { CachingConnection.Cache = value; }
}
public CachingPolicy CachingPolicy
{
get { return CachingConnection.CachingPolicy; }
set { CachingConnection.CachingPolicy = value; }
}
#endregion
}
}
我可能要用DbContext创建一个公平的说法吗?
我可以使用.Entry以外的任何东西吗?我很感激帮助,但是对于应用程序的设置方式,我不确定如何正确地将编辑保存回数据库。
答案 0 :(得分:0)
除了编辑动作的.Entry之外还有一种方法。 EF非常聪明,您可以通过ID从数据库中获取对象并手动更改所有属性。 例如:
[HttpPost]
public ActionResult Edit(ITTESI.AssetTracker.Web.UI.ViewModels.AssetDetailsViewModel models)
{
try
{
if (ModelState.IsValid)
{
ITTESI.AssetTracker.Web.UI.ViewModels.AssetDetailsViewModel temp = _entities.dbname.firstOrDefault(x = > x.ID == models.ID);
temp.AssetIdentifier = models.AssetIdentifier ;
temp.ManufacturerName = models.ManufacturerName ;
.
.
.
_entities.SaveChanges();
return RedirectToAction("Index", new { ID = models.ID });
}
}
catch (DataException)
{
ModelState.AddModelError("", "Unable able to save changes....epic fail!!!");
}
return View(models);
}
EF将了解您要修改的实体,并将使用“_entities.saveChanges();”进行更新。 我真的不知道这对你有用,但我认为值得尝试