我向 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 以外的任何东西吗?感谢您的帮助,但由于应用程序的设置方式,我不确定如何将编辑正确保存回数据库。