Sql server SQL表中具有主标识键列的更新方法和视图的MVC问题
我正在制作简单的应用程序,比如通讯录 我通过实体框架用一个表将我的项目与SQL数据库连接起来。SQL表中的第一行是ID,它是主键和标识字段。当我自动生成编辑视图并尝试运行它时,出现以下错误: 参数字典包含“akvizicje_App_1_6.Controllers.HomeController”中方法“System.Web.Mvc.ActionResult UpdateInt32”的不可为null类型“System.Int32”的参数“id”的null条目。可选参数必须是引用类型、可为null的类型或声明为可选参数。 参数名称:参数 我知道有一个参数具有int id,它不能为null,因为它是主键和标识字段 但我不知道如何解决这个问题。如果有人能帮我Sql server SQL表中具有主标识键列的更新方法和视图的MVC问题,sql-server,asp.net-mvc,model-view-controller,Sql Server,Asp.net Mvc,Model View Controller,我正在制作简单的应用程序,比如通讯录 我通过实体框架用一个表将我的项目与SQL数据库连接起来。SQL表中的第一行是ID,它是主键和标识字段。当我自动生成编辑视图并尝试运行它时,出现以下错误: 参数字典包含“akvizicje_App_1_6.Controllers.HomeController”中方法“System.Web.Mvc.ActionResult UpdateInt32”的不可为null类型“System.Int32”的参数“id”的null条目。可选参数必须是引用类型、可为null的
[HttpPost]
public ActionResult Update(Kontakti model)
{
if (!ModelState.IsValid)
{
return View("Index", model);
}
Context.Entry(model).State =
System.Data.Entity.EntityState.Modified;
Context.SaveChanges();
return RedirectToAction("Details");
}
这就是观点
@model Akvizicije_App_1_6.Kontakti
@{
ViewBag.Title = "Edit"; }
<h2>Edit</h2>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Kontakti</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.idKontakt)
<div class="form-group">
@Html.LabelFor(model => model.Zupanija, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Zupanija, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Zupanija, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Opcina, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Opcina, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Opcina, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.PostanskiBroj, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.PostanskiBroj, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.PostanskiBroj, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.NazivNaselja, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.NazivNaselja, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.NazivNaselja, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.NazivUlice, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.NazivUlice, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.NazivUlice, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.KucniBroj, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.KucniBroj, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.KucniBroj, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.kcbr, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.kcbr, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.kcbr, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Aktivan, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
@Html.EditorFor(model => model.Aktivan)
@Html.ValidationMessageFor(model => model.Aktivan, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ImeOsobe, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ImeOsobe, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ImeOsobe, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.PrezimeOsobe, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.PrezimeOsobe, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.PrezimeOsobe, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.KontaktBroj, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.KontaktBroj, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.KontaktBroj, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Komentar, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Komentar, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Komentar, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div> }
<div>
@Html.ActionLink("Natrag", "Index") </div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval") }
SQL数据库中的模型
web配置连接字符串
和控制器的第一部分
我换了标签。谢谢您在哪里调用Update方法?是否正在传递名为“id”的int值?
@model Akvizicije_App_1_6.Kontakti
@{
ViewBag.Title = "Edit"; }
<h2>Edit</h2>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Kontakti</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.idKontakt)
<div class="form-group">
@Html.LabelFor(model => model.Zupanija, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Zupanija, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Zupanija, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Opcina, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Opcina, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Opcina, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.PostanskiBroj, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.PostanskiBroj, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.PostanskiBroj, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.NazivNaselja, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.NazivNaselja, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.NazivNaselja, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.NazivUlice, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.NazivUlice, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.NazivUlice, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.KucniBroj, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.KucniBroj, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.KucniBroj, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.kcbr, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.kcbr, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.kcbr, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Aktivan, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
@Html.EditorFor(model => model.Aktivan)
@Html.ValidationMessageFor(model => model.Aktivan, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ImeOsobe, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ImeOsobe, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ImeOsobe, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.PrezimeOsobe, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.PrezimeOsobe, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.PrezimeOsobe, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.KontaktBroj, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.KontaktBroj, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.KontaktBroj, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Komentar, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Komentar, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Komentar, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div> }
<div>
@Html.ActionLink("Natrag", "Index") </div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval") }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace Akvizicije_App_1_6
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
}
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace Akvizicije_App_1_6
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
public partial class Kontakti
{
[Key]
public int idKontakt { get; set; }
public string Zupanija { get; set; }
public string Opcina { get; set; }
public int PostanskiBroj { get; set; }
public string NazivNaselja { get; set; }
public string NazivUlice { get; set; }
public string KucniBroj { get; set; }
public string kcbr { get; set; }
public bool Aktivan { get; set; }
public string ImeOsobe { get; set; }
public string PrezimeOsobe { get; set; }
public string KontaktBroj { get; set; }
public string Email { get; set; }
public string Komentar { get; set; }
}
}
<connectionStrings>
<add name="AkvizicijeKontaktiEntities" connectionString="metadata=res://*/Model.csdl|res://*/Model.ssdl|res://*/Model.msl;provider=System.Data.SqlClient;provider connection string="data source=SVEN\SQLEXPRESS;initial catalog=AkvizicijeKontakti;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
public class HomeController : Controller
{
AkvizicijeKontaktiEntities Context = new AkvizicijeKontaktiEntities();
public ActionResult Index()
{
List<Kontakti> model = (from k in Context.Kontakti
select k).ToList();
return View(model);
}
public ActionResult Create()
{
return View();
}