已添加具有相同键的项目。错误

时间:2011-06-16 12:06:44

标签: asp.net-mvc asp.net-mvc-3 asp.net-mvc-2

这是我的控制器类。

namespace MvcApplication7.Controllers
{
    public class HomeController : Controller
    {
        private rikuEntities db = new rikuEntities();
        public ActionResult Index()
        {
            ViewBag.Message = "Welcome to ASP.NET MVC!";

            return View();
        }

        public ActionResult About()
        {
            return View();
        }
        public ActionResult create()
        {
            rikuEntities db = new rikuEntities();

            ViewBag.saan = new SelectList(db.emp, "Id", "name");

            return View();


        }
        [HttpPost]
        public ActionResult create(login lo)
        {
            if (ModelState.IsValid)
            {
                db.login.AddObject(lo);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.saan = new SelectList(db.emp, "Id", "name", lo.Id);
            return View(lo);
        }
    }
}

这是View create.cshtml

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>login</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.name)
        </div>
        <div class="editor-field">
            @Html.DropDownList("saan","Select");
            @Html.ValidationMessageFor(model => model.name)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.fname)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.fname)
            @Html.ValidationMessageFor(model => model.fname)
        </div>

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

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

实际上我有一个下拉列表,其中的值从表 \ temp 加载。 和一个文本框。现在我想在另一个表登录中保存这些值(下拉选定值和文本输入值)。但每当我按下创建按钮时都会出现问题“已经添加了具有相同键的项目。”

请告诉我我做了什么错误。我在两个表中都创建了自动生成的ID。请建议我,紧急。

0 个答案:

没有答案