开发者

asp.net mvc scaffolding and GUID as the primary key

开发者 https://www.devze.com 2023-03-21 13:35 出处:网络
I have a table with the following structure GUID uniqueidentifier with default value newid(), and set as primary key

I have a table with the following structure

GUID uniqueidentifier with default value newid(), and set as primary key
ID int
Description varchar(max)

I created an Entity Model using visual studio, and generated the views for editing/deleting etc (mvc scaffolding)

The problem is with "Editing", when I click that link, the appropriate form is showed with the correct data, but the "save" button doesn't work at all. Please note that other links (delete,create,details) work perfectly..

So, when I click the "Edit" link, the url is

http://localhost:10871/admin/Edit/e7d0c5ee-7782-411f-920e-7b0d93c924e1

and the form is displayed correctly, but the save button doesn't work, no network activity happens. Is something particular about using uniqueidentifers as primary key?

Thanks

---C开发者_如何学运维ode---

Edit.cshtml

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

        @Html.HiddenFor(model => model.GUID)

        @Html.HiddenFor(model => model.ID)

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

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

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

--AdminController.cs--

// GET: /Admin/Edit/5

public ActionResult Edit(Guid id)
{
    Domain domain = db.Domains.Single(d => d.GUID == id);
    return View(domain);
}

//
// POST: /Admin/Edit/5

[HttpPost]
public ActionResult Edit(Domain domain)
{
    if (ModelState.IsValid)
    {
        db.Domains.Attach(domain);
        db.ObjectStateManager.ChangeObjectState(domain, EntityState.Modified);
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    return View(domain);
}

Edit2 In response to a comment by ZippyV, I added the following code in Edit.cshtml

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

to my surprise (or ignorance) - the GUID is being shown instead of ID

asp.net mvc scaffolding and GUID as the primary key

and apart from that - when I enter a value in that field (1,2 or any integer), I still get the message "The field ID must be a number."


The problem comes from the fact that you have a property called ID on your view model:

public class Domain
{
    public int ID { get; set; }
    ...
}

and at the same time you have a route parameter called id in your Edit controller action:

public ActionResult Edit(Guid id)
{
    ...
}

Now here's what happens: The Html helpers (such as TextBoxFor, HiddenFor, ...) always first looks in the model state when binding a value (i.e. query string parameters, route values) and only at the end it looks at the value in the model. That's how all Html helpers work and it is by design.

So you have the following:

@Html.HiddenFor(model => model.ID)

The ID is taken NOT from the model but from your Route data (which is a Guid as specified in your action parameter). That's why it is very dangerous to use properties in the view model and action parameters that have the same name.

One possible workaround would be to rename your action parameter to something else:

public ActionResult Edit(Guid domainId)
{
    Domain domain = db.Domains.Single(d => d.GUID == domainId);
    return View(domain);
}

Of course now your routes might need to be adapted or use query string parameters: controller/edit?domainId=d578b4b7-48ec-4846-8a49-2120c17b6441 to invoke the Edit action.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号