简体   繁体   English

将模型从视图传递到控制器(MVC)

[英]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; 我可以使用ActionLink成功传递它,但我认为这不是最好的方法。 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; 如果要发布到的操作(HttpPost Admin / Manage操作)采用类型为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. 从模型创建参考(即x)将达到目的。

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: 我在使用HiddenFor时遇到了麻烦,因此我过去直接使用了隐藏:

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. 提交表单后,该表单中的用户将通过模型以及这些新属性回发。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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