Model Mvc id 0 per position

I have an edit page for my model. When I return the model back to my controller, the Id property is 0. How can I save the Id property in the message?

I submit the model for viewing, so the Id property is set in the get request. My code is below.

View:

@model xandra.Models.ProjectModel @using(Html.BeginForm()) { @Html.HiddenFor(m => m.Id) @Html.LabelFor(m => m.Name) @Html.TextBoxFor(m => m.Name) <br /> @Html.LabelFor(m => m.Description) @Html.TextAreaFor(m => m.Description) <br /> @Html.LabelFor(m => m.Website) @Html.TextBoxFor(m => m.Website) <br /> @Html.LabelFor(m => m.Active) @Html.CheckBoxFor(m => m.Active) <input type="submit" value="Save" /> } 

Controller:

 [Authorize] [HttpGet] [InitializeSimpleMembership] public ActionResult EditProject(string id) { using (var entity = new dixraContext()) { var project = entity.Projects.FirstOrDefault(m => m.UrlName == id); return View(project); } } [Authorize] [HttpPost] [InitializeSimpleMembership] public ActionResult EditProject(ProjectModel model) { using (var entity = new dixraContext()) { var project = entity.Projects.FirstOrDefault(m => m.Id == model.Id); project.Name = model.Name; project.Description = model.Description; project.Website = model.Website; project.Tags = model.Tags; project.Active = model.Active; entity.SaveChanges(); return RedirectToAction("GetProject", project.UrlName); } } 

And my model

 public class ProjectModel { [Key] [Required] public long Id { get; set; } [Required(AllowEmptyStrings=false)] public string Name { get; set; } [Required(AllowEmptyStrings=false)] public string Description { get; set; } [Required] public DateTime CreatedDate { get; set; } [Required] public int Creator { get; set; } public string Website { get; set; } [Required] public int CategoryId { get; set; } public virtual List<TagModel> Tags { get; set; } public string UrlName { get; set; } public bool Active { get; set; } } 
+6
source share
4 answers

I have seen this before, it could be a mistake. I was able to get around this by adding properties that I need to include in the message without using the syntax of a specific view template.

Instead :

 @Html.HiddenFor(m => m.Id) 

Use this one :

 <input type="hidden" value="@Model.Id" name="Id"/> 
+5
source

I believe this is because asp.net MVC always made the model return to the user as it was sent to the server. You can override this behavior using the following attribute ...

 void ControllerAction([Bind(Exclude = "Id")]SomeModel model) { //action stuff here } 
+1
source

I had the same problem. In the base column of the database table, there was no identification specification, for example. int identity (1,1). After committing, the entity infrastructure stopped setting the id column to 0.

0
source

@Sefa

You are missing an identifier in a controller action.

project.id = model.id; (is absent)

 project.Name = model.Name; project.Description = model.Description; 
0
source

Source: https://habr.com/ru/post/974051/


All Articles