2

I seem to be having difficult when I try to pass an object from my View to my HttpPOST method.

Upon clicking the button in my View, the code hits the POST method but no data is being passed through. I have tried updating the method parameters and passing different values (like Ints) but to no avail.

Here is my HttpPOST method:

    [HttpPost]
    public async Task<ActionResult> BookClass(MemberClassViewModel memberClassViewModel)
    {
        return RedirectToAction("Index");
    }

The parameter passed in always seems to be NULL.

Here is the code for my View. The Model is of type IEnumerable i.e a list of my ViewModel objects, which in my case is 3 objects. What I am trying to achieve is, clicking on the appropriate button, will result in that one object being passed back to my HttpPOST method. I've tried passing in various values in the 'value' parameter.

@model IEnumerable<GymTracker.ViewModel.MemberClassViewModel>


@{
    ViewData["Title"] = "BookClass";
}

<h2>Book Class</h2>

@foreach (var item in Model)
{
    <form  method="post">    
        <div>    
            <hr />
            <dl class="dl-horizontal">    
                <dt>
                    @Html.DisplayNameFor(model => model.ClassName)    
                </dt>
                <dd>
                    @Html.DisplayFor(modelItem => item.ClassName)
                </dd>
                <dt>    
                    <button>Book Class</button>
                    <input type="hidden" asp-action="BookClass" asp-controller="Members" value="@item"/>    
                </dt>        
            </dl>
        </div>    
    </form>    
}

enter image description here Any help will be appreciated,

Thanks

9
  • Use <input type="submit" /> to submit you form data. Commented Jun 15, 2018 at 8:45
  • you should use foreach since you have bind a model list to view Commented Jun 15, 2018 at 8:46
  • Possible duplicate of ASP.Net MVC How to pass data from view to controller Commented Jun 15, 2018 at 8:48
  • I have amended the input type and removed the button. However no value is being passed back to my HttpPOST method still. I have followed the link above to the other question but to no avail, Commented Jun 15, 2018 at 9:11
  • 2
    You are not generating any form controls so there is nothing to submit Commented Jun 15, 2018 at 9:21

1 Answer 1

3

I have re-written your form a little bit more bare so you can understand that you need the name field to relate to your naming on the controller side.

If you have a MyModel.PropertyName Model object, it must have some kind of attributes/properties. what you are doing now with item is you are trying to store MyModel in an input field which will never work. MyModel needs an attribute let's call it MyAttribute which can be the name (string) or id (int) to relate to a unique book.

@foreach (var item in Model)
{
    <form asp-action="BookClass" asp-controller="Members" method="post">
        <button type="submit">Book Class</button>
        <input type="hidden" name="MyModel.MyAttribute" value="@item.MyAttribute"/>      
    </form>  
}

Edit:

I see what you mean, so what I am trying to do is, if the user clicks on a certain button for a particular Gym class, that gets posted back to my method. I then do my SQL writes etc (to update this class to say +1 member has booked this).

what you can do is:

@foreach (var gym in Model)
{
    <a asp-action="BookClass" asp-controller="Members" asp-route-id="@gym.Id">@gym.Name</a>
}

And in your controller:

[HttpGet([controller]/[action]/{id})]
public ActionResult BookClass(int id)
{
    //get the gym based on the id

    return RedirectToAction("Index");
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, so you mean I can't send the entire object back to the controller? With all of its attributes? I will try your suggestion by just trying to return just a single attribute
@Metzer nope, you can't store a whole object in an input field, you need to store it's attributes individually.
Right got it, Thanks for your help I will have a play around.
I guess there is no way to store multiple attributes? Say for example my object had 3 attributes, if there is no way to pass the entire object back, I'd like to pass back the 3 individual attributes. Is this possible
@Metzer depend's what kind of attributes you can try to store them in a single string and separate them with a '|' for example, and have the logic in the controller to separate it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.