locked
passing data from view to controller, utilizing model view parameter, and input parameter RRS feed

  • Question

  • User-1599526732 posted

    Hello, 

    i am new to MVC and I am using a model view which passes an item from a repository into a creation view, the item has a ID which I want to pass into the controller parameters. I also want to pass a Quantity value which is an input from the user to fulfil the other parameter of the asp-action. I am not sure how to implement this idea, the previous code is in @* *@ where I used to have a quantity variable within but I want to change the implementation.

    @model MvcStore.Models.Pet
    
    <div class="col-md-4">
            <form asp-action="CreateConfirmed">
            <input type="hidden" asp-for="PetId" />
            
           @* <div class="form-group">
                <label asp-for="Quantity" class="control-label"></label>
                <input asp-for="Quantity" class="form-control" />
                <span asp-validation-for="Quantity" class="text-danger"></span>
            </div> *@
            <input type="submit" value="Add To Cart" class="btn btn-danger" /> |
            <a asp-action="Index">Back to Shop View</a>
    public async Task<IActionResult> CreateConfrimed(int id, int Quantity)

    And this is the parameter I want to fulfil, any ideas?

    Wednesday, March 10, 2021 11:11 AM

All replies

  • User475983607 posted

    You named the Id PetId not Id. 

    <input type="hidden" asp-for="PetId" />

    Change the input paraemter.

    public async Task<IActionResult> CreateConfrimed(int PetId)

    I strongly recommend going through beginning level tutorials to learn MVC programming basics.

    https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/start-mvc?view=aspnetcore-5.0&tabs=visual-studio

    https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/?view=aspnetcore-5.0

    Wednesday, March 10, 2021 11:50 AM
  • User1429838465 posted

    Hey eddy28,

    There are a couple of ways to pass data back to the controller.

    • Passing Variables (in previously mentioned comment)
    • FormCollection
    • ViewModels

    I've always leaned towards a ViewModel for holding most of my data. This approach avoids an overloaded Action in the controller taking in 15 parameters.

    I talk more about this on my site.

    3 Ways To Receive Data from "Postbacks" in ASP.NET MVC

    Hope this helps.

    JD 

    Wednesday, March 10, 2021 12:33 PM
  • User-1599526732 posted

    You named the Id PetId not Id. 

    <input type="hidden" asp-for="PetId" />

    Change the input paraemter.

    public async Task<IActionResult> CreateConfrimed(int PetId)

    I strongly recommend going through beginning level tutorials to learn MVC programming basics.

    https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/start-mvc?view=aspnetcore-5.0&tabs=visual-studio

    https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/?view=aspnetcore-5.0

    Maybe I didn't explain it well enough, I wanted to use the Id for the model which is PetId, as a parameter for Create Confirmed. This would draw from a repository so I could recreate the model information in my "shopping cart". This code seems to work and PetId fulfils the Id parameter in my CreateConfirmed code. If I use replace PetId with Id in that parameter, there is an error:

    "'Pet' does not contain a definition for 'Id' and no accessible extension method 'Id' accepting a first argument of type 'Pet' could be found (are you missing a using directive or an assembly reference?) [MvcStore]"

    What I need help with is, then next passing an input of "Quantity" from the user which would specify how much of this item they would buy without referencing the model, and passing it into the parameter for CreateConfirmed. I hope that clears it up more.

    Wednesday, March 10, 2021 8:05 PM
  • User475983607 posted

    You are not following model binding rules.  Please go through the tutorial in my fist post rather than guessing how the technology works.  Also I recommend dropping the repository pattern.  Entity Framework is already a repository.  

    Thursday, March 11, 2021 12:31 AM