简体   繁体   中英

C# MVC - Passing ViewModel object from View to Controller

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 ie 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>    
}

在此处输入图片说明 Any help will be appreciated,

Thanks

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");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM