2033
2033

Reputation: 1

The view automatically populates the ID property of the view model, though I didn't ask about it

The main problem is that my view for some reason populates the view model's ID property. It takes it from SetId property of this view model (as I can tell).

// Primary view model
public class SetViewModel
{
    public int Id { get; set; }

    public string Name { get; set; } = null!;

    // Navigation property
    public List<Word>? Words { get; set; } = [];
}

There is a button which leads me to another view with a get method below it. In this button I'm passing the id in order to have this id field in the view for back button.

<div class="float-end">
    <a asp-area="Sets" asp-controller="Home" asp-action="AddNewWord" asp-route-id="@Model.Id" type="button">
        Add new word
    </a>
</div>

Get method: basically I want my next view be with the SetId property, but not with id. And the view for some reason populates id also with the same value.

public IActionResult AddNewWord(int id)
{
    if (id is 0)
        return NotFound();

    var wordVM = new WordViewModel { SetId = id };

    return View(wordVM);
}

Now the view and it's view model:

// Secondary view model
public class WordViewModel
{
    public int Id { get; set; }

    public string Name { get; set; } = "";
    public string Definition { get; set; } = "";
    public string ImagePath { get; set; } = "";

    // Navigation property
    public int SetId { get; set; }
}

Here is this problem view:

@model WordViewModel

<form asp-action="AddNewWord" method="post">
    <div>
        <div>
            <h2>Create new word</h2>
            <hr />
        </div>

        <div>
            <input asp-for="Name">
        </div>

        <div>
            <input asp-for="Definition">
        </div>

        <div>
            // Here I'm telling the view populate the setId property.
            <div>
                <button asp-route-SetId="@Model.SetId" type="submit">Create word</button>
            </div>

            // Back button is the reason I wanted this SetId property be filled from the beginning
            <div class="col-6 col-md-3 m-auto">
                <a asp-area="Sets" asp-controller="Home" asp-action="SelectedSet" asp-route-id="@Model.SetId">Back</a>
            </div>
        </div>
    </div>
</form>

Unfortunately I cannot embed images.

It's the get method, I want my view's model be with defined setId property for back button

https://drive.google.com/file/d/1qs7bmAWuYm-YNcLH7YxVyJJY6BEHq98V/view?usp=drive_link

Results: as soon as I submit the form, I have this situation:

https://drive.google.com/file/d/1vrTOOPQZ0KoI0MuNTgsyAup6JbR-YU4g/view?usp=drive_link

The id should be 0.

Upvotes: 0

Views: 37

Answers (1)

Ruikai Feng
Ruikai Feng

Reputation: 11896

When you click Create word button,the post request would be sent with the url:

enter image description here

And the value 14 would match the id section in your default route parttern,and been binded to the Id property in your viewmodel

Try modify the default route parttern in your program.cs:

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

to

app.MapControllerRoute(
        name: "default",
        pattern: "{controller=Home}/{action=Index}");

Or modify

public int Id { get; set; }

to

public int ViewModelId { get; set; }

It's ok on myside: enter image description here

Upvotes: 0

Related Questions