Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mvc model id 0 on post

I have an edit page for my model. When i post model back to my controller, Id property is 0. How can i maintain Id property on post?

I'm posting model to view, so Id property is set on 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; }
}
like image 661
Sefa Avatar asked Aug 19 '14 20:08

Sefa


People also ask

Is there postback in MVC?

There is no such thing as a stateful postback where things just automatically retain their state. You only have ViewModels that are bound to the view, and ViewModels that are posted by the view to the Controller.

What is mvc4?

ASP.NET MVC 4 is a framework for building scalable, standards-based web applications using well-established design patterns and the power of the ASP.NET and the . NET framework. This new, fourth version of the framework focuses on making mobile web application development easier.

What is model IsValid MVC?

ModelState. IsValid indicates if it was possible to bind the incoming values from the request to the model correctly and whether any explicitly specified validation rules were broken during the model binding process. In your example, the model that is being bound is of class type Encaissement .

What is model in MVC with example?

The model contains all the data-related logic that the user works with, like the schemas and interfaces of a project, the databases, and their fields. For example, a customer object will retrieve the customer information from the database, manipulate or update their record in the database, or use it to render data.


2 Answers

I've seen this happen before, it could be a bug. I have been able to work around it by adding the properties I need to be included in the post by not using the specific's view template syntax.

Instead of this:

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

Use this:

<input type="hidden" value="@Model.Id" name="Id"/> 
like image 171
Ricardo Sanchez Avatar answered Sep 20 '22 18:09

Ricardo Sanchez


Had the same problem. Here is solution: In controller class in action function (handled posted data) add line:

ModelState.Remove("Id");

If you don't do it, any value assigned by you to the Id property in the Model will be replaced with the value from POST request. So your Id change in the Model has no effect without removing it from ModelState.

like image 34
Jarek L Avatar answered Sep 16 '22 18:09

Jarek L