集成模型和控制器 [英] Integrating the Model and Controller
本文介绍了集成模型和控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Hello, I have the following code:
I have a problem creating the new record in the database, I get the following error:
System.NotImplementedException: Unable to implement the method or operation.
and wanted to know if it is properly introduced the following piece of code
_db.Add (Palabras_user), because that's where I have the problem
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using RForzadas.Models;
namespace RForzadas.Controllers
{
public class HomeController : Controller
{
private RForzadasBDEntities _db = new RForzadasBDEntities();
//
// GET: /Home/
public ActionResult Index()
{
return View(_db.RForz);
}
//
// GET: /Home/Create
public ActionResult Create()
{
return View();
}
//
// POST: /Home/Create
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(HomeController Palabras_user)
{
if (!ModelState.IsValid)
{
return View("Create", Palabras_user);
}
<big>_db.Add(Palabras_user);</big>
return RedirectToAction("Index");
}
public ViewResult About()
{
throw new NotImplementedException();
}
}
}
您可以通过创建" Tengo el siguientecódigo:
En el archivo "Create" tengo el siguiente código:
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Create</h2>
<%= Html.ValidationSummary("Create was unsuccessful. Please correct the errors and try again.") %>
<% using (Html.BeginForm()) {%>
<fieldset>
<legend>Fields</legend>
<p>
<label for="Palabras user">Palabras user:</label>
<%= Html.TextBox("Palabras User") %>
<%= Html.ValidationMessage("Palabras user", "*") %>
</p>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
<% } %>
<div>
<%=Html.ActionLink("Back to List", "Index") %>
</div>
</asp:Content>
如果可以帮助您确定问题,我将不胜感激.
感谢和问候.
If I could help identify the problem I''d appreciate it.
Thanks and regards.
推荐答案
从RForzadasBDEntities()创建的对象_db没有Add方法.如果看到此对象的Add方法,它是Public吗?如果看到Public方法,它是否接受您要传递的参数类型?
The object _db created from RForzadasBDEntities() does not have an Add method. If you see the Add method on this object, is it Public? If you see a Public method, does it accept the type of argument you are passing it?
检查Post方法
Check your Post method
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(HomeController Palabras_user)
您不应该将控制器发布到此方法,它应该是视图绑定到的模型对象,例如
You shouldn''t be posting the controller to this method, it should be the model object that your view is bound to, e.g.
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(UserView Palabras_user)
您不能直接将任何实体添加到数据库上下文中.进入关联的表,然后添加项目.方法显示在Entities上下文中,它会抛出错误msot的时间.可能如果您可以在以下操作中更改代码,则可能会起作用
db.< tables> .Add(< tablerowentity>)
如果您确信由于_db.Add方法
You Cannot directly add to the DB context any entity.So you have to get into the associated table and then add the item.Though the Add <> method is displayed in the Entities Context,it throws error msot of the times.Probably if you could change the code in the following manned it may work
db.<tables>.Add(<tablerowentity>)
Please check if this works if you are assured that problem arises due to the _db.Add Method
这篇关于集成模型和控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文