Skip to content

Asp.net core putting the same Guid for all empty Guid's #1926

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jrrcdev opened this issue Feb 5, 2017 · 7 comments
Closed

Asp.net core putting the same Guid for all empty Guid's #1926

jrrcdev opened this issue Feb 5, 2017 · 7 comments

Comments

@jrrcdev
Copy link

jrrcdev commented Feb 5, 2017

Hello, guys.

I'm going through a problem when using Guid as Id, when I have Entity like:

`
public class SubcategoryDto
{
public Guid Id { get; set; }

    public string Description { get; set; }

    public Guid CategoryId { get; set; }
}`

and I have a Action in my controller with:

public IActionResult Create(Guid id) { var dto = new SubcategoryDto() { CategoryId = id }; return View(dto); }

I expect in my View I receive the follow object:

{
"id": "00000000-0000-0000-0000-000000000000",
"description": "",
"categoryid": "dba6b8e4-fb16-4f88-290d-08d44abea368"
}

But I receive:

{
"id": "dba6b8e4-fb16-4f88-290d-08d44abea368",
"description": "",
"categoryid": "dba6b8e4-fb16-4f88-290d-08d44abea368"
}

So, is it a issue or I need to do some configuration?

@Tratcher
Copy link
Member

@rynowak ?

@rynowak
Copy link
Member

rynowak commented Feb 10, 2017

We don't have any magic around guids. I don't see anything in the code sample that sets the Id value thus it should always be empty.

Do you have a more complete code sample including the view, and ideally what the request looks like?

@jrrcdev
Copy link
Author

jrrcdev commented Feb 12, 2017

Here is a test repo:
https://github.com/jonatanrinckus/AspNetCoreGuidIssueTest

the HomeController has this action:

public IActionResult Edit(Guid id) { var test = new Test() { AnotherGuidId = id }; return View(test); }
if you go to /home/edit/4689d77b-a373-450d-a7e6-3394b557ce32
It you bring the Guid on both inputs, but this should me just on one.

And it just happens if the name of parameter is "id".

if I add a [Route("{id}"] attribute, the same happens.

If i change for another parameter name like "idt" and [Route("{idt}"], it works like expected.

@rynowak
Copy link
Member

rynowak commented Feb 13, 2017

This happens because entries in the ModelState take precedence over the values in the model. This is needed because when you submit a form and are sent back to the form (because of validation errors) you want to see the inputs that you typed in, and not the values on the model.

/cc @dougbu in case he wants to add more

When you successfully bind the id route value, it creates an entry in the ModelState for that value. If you don't want to see that on the form, just clear it.

        public IActionResult Edit(Guid id)
        {
            var test = new Test()
            {
                AnotherGuidId = id
            };

            ModelState.Remove("id");

            return View(test);
        }

@aspnet-hello
Copy link

This issue is being closed because it has not been updated in 3 months.

We apologize if this causes any inconvenience. We ask that if you are still encountering this issue, please log a new issue with updated information and we will investigate.

@davidalpert
Copy link

davidalpert commented Dec 30, 2017 via email

@davidalpert
Copy link

davidalpert commented Dec 30, 2017 via email

@ghost ghost locked as resolved and limited conversation to collaborators Dec 4, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants