简体   繁体   中英

Pass model from a view to a controller (MVC)

When passing the model from my view to my controller, the data is all null. I was able to successfully pass it using an ActionLink but I don't think that is the best way; for security reasons (I do not want sensitive data in the querystring).

My models

public class DashboardModel
{
    // Dasboard quick numbers
    public int TotalUsers { get; set; }
    public int TotalUnauthUsers { get; set; }
    public int GamesPlayed { get; set; }
    public int AssociatedGroups { get; set; }
    public int TotalGroups { get; set; }

    // Dashboard table
    public IEnumerable<ManageUserData> UnauthUsers { get; set; }

}

public class ManageUserData
{
    public string UserName { get; set; }
    public int AlternateId { get; set; }
    public string Email { get; set; }
    public string Role { get; set; }
    public IEnumerable<string> InvestigatorGroups { get; set; }
    public string Institution { get; set; }

    // User status
    public bool AccountLocked { get; set; }
    public bool EmailConfirmed { get; set; }
}

Snippet of my view

@model TestGame.ViewModels.DashboardModel

@foreach (var user in Model.UnauthUsers)
{
    <tr>
        <td>@user.UserName</td>
        <td>@user.AlternateId</td>
        <td>@user.Email</td>
        <td>@user.Role</td>
        <td>
            @if (!user.EmailConfirmed)
            {
                <div class="text-warning">Unconfirmed Email</div>
            }
            @if (user.AccountLocked)
            {
                <div class="text-danger">Account Locked</div>
            }

        </td>
        <td>

            @if (user.AccountLocked || !user.EmailConfirmed)
            {

                using (Html.BeginForm("Manage", "Admin", FormMethod.Post))
                {
                    @Html.HiddenFor(x => user.UserName)
                    @Html.HiddenFor(x => user.Email)

                    <input type="submit" value="Manage" />

You have to make sure the path starts with the object being posted back; what you have will work great if the action being posted to (HttpPost Admin/Manage action) takes an object of type User; if it takes an object of the model type, change your form to the following:

for (var i = 0; i < Model.UnAuthUsers.Count; i++)

 using (Html.BeginForm("Manage", "Admin", FormMethod.Post))
                {
                    @Html.HiddenFor(x => x.UnAuthUsers[i].UserName)
                    @Html.HiddenFor(x => x.UnAuthUsers[i].Email)

                    <input type="submit" value="Manage" />

Creating a reference from the model (being x) will do the trick.

EDIT: Based on comments, add two properties to your model:

public class DashboardModel
{
   public string SelectedUserName { get; set; }
   public string SelectedEmail { get; set; }
}

In your form, render a hidden for for that name; I've had trouble using HiddenFor, so I've in the past used the hidden directly:

using (Html.BeginForm("Manage", "Admin", FormMethod.Post))
{
   <input type="hidden" name="@Html.NameFor(i => i.SelectedUserName)" value="@Model.UnauthUsers[i].UserName" />
   <input type="hidden" name="@Html.NameFor(i => i.SelectedEmail)" .. />

And upon submitting the form, the user from that form will be posted back via the model, via these new properties.

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