I am making simple app like address book.
I connected my project with SQL database with just one table via Entity Framework. The first row in the SQL table is ID which is primary key and identity field. When I auto generate edit View and try to run it, I got this error:
The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Update(Int32)' in 'Akvizicije_App_1_6.Controllers.HomeController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters "
[HttpGet]
public ActionResult Update(int id)
{
Kontakti kontakt = Context.Kontakti.Find(id);
return View("Update", kontakt);
}
I know there is parameter with int id and it can't be null because it is primary key and identity field.
But I don't know how to solve that problem. If someone can help me.
[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");
}
This is the view
@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") }
ROUTE CONFIG
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 }
);
}
}
}
MODEL FROM SQL DATABASE
//------------------------------------------------------------------------------
// <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; }
}
}
web config conn string
<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" />
AND first part of controller
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();
}