如何将对玩家列表的更改发布到数据库中?

时间:2018-08-02 09:44:12

标签: c# asp.net asp.net-mvc

我想将数据发布到数据库,但是将其发布到控制器时我的视图模型为空,我尝试了不同的方法,但是每次我的视图模型为null时。

这些是我的课程:

 public class Player
 {      
    [Key]
    public int Id { get; set; }

    public string Name{ get; set; }

    public string PreName{ get; set; }
 }

public class Activitity
{
    [Key]
    public int Id { get; set; }

    public string WhichActivity { get; set; }

    public List<Player> Players { get; set; }
}

public class Aanwezigheid
{
    [Key]
    public int Id { get; set; }
    public ReasonEnum Reason{ get; set; }

    public int PlayerId{ get; set; }
    public Player Player{ get; set; }
    public List<Player> Players{ get; set; }

    public int ActivityId { get; set; }
}

我的视图模型:

public class PresenceVM
{
    public int PlayerId{ get; set; }

    public int ActivityId { get; set; }

    public string Name{ get; set; }

    public string PreName { get; set; }

    public ReasonEnum Reason { get; set; }
}

我的球员的HTTPGET,我想将缺席原因与球员一起放入数据库。

[HttpGet]
public ActionResult Presence(int id)
{
    var sp = _context.Players.ToList();

    foreach(Players s in sp)
    {
        var van = new PresenceVM
        {
            PlayerId = s.Id,
            Name = s.Name,
            PreName = s.PreName,
            ActivityId = id
        };

        list.Add(van);
    }

    return View(list);
}

我的HttpPost

[HttpPost]
public ActionResult Presence(List<PresenceVM> list)
{
    var sp = _context.Players.ToList();
    var list = new List<Presence>();

    foreach (Players s in sp)
    {
        var van = new Aanwezigheid
        {
            PlayerId = s.Id,
            ActivityId = vm.ActivityId,
            Reason = vm.Reason
        };

        list.Add(van);
        _context.Presence.Add(van);
        //_context.SaveChanges();
    }

    return RedirectToAction("Index", "Presence");
}

问题是我的PresenceVm(视图模型)没有获得真正的控制器中的任何数据。我不明白为什么?是因为清单吗?只需一项,就可以轻松地将其发布到数据库。也许有多个项目?

编辑1:

获取和发布的视图模型

@model IEnumerable<....ViewModels.PresenceVM>

@{
    ViewBag.Title = "Presence";
}

<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.PreName)
        </th>
        <th>
            @Html.DisplayName("Reason")
        </th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Name)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.PreName)
            </td>
            <td>
                @Html.EnumDropDownListFor(modelItem => item.Reason, "Present", new { @class = "form-control" })
            </td>
        </tr>
    }

</table>

<form action="/Presences/Presence" method="post">
    <div class="form-horizontal form-details">
        <input type="submit" value="Save" class="btn btn-default" />
    </div>
</form>

3 个答案:

答案 0 :(得分:1)

您将表单范围放在错误的位置(仅包含提交按钮)。正确的方法应该包括要与索引一起提交的所有属性(因为使用IEnumerable<PresenceVM>,例如以下示例:

<form action="/Presences/Presence" method="post">
    <table class="table">
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Name)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.PreName)
            </th>
            <th>
                @Html.DisplayName("Reason")
            </th>
        </tr>
    @for (var i = 0; i < Model.Count; i++)
    {
        <tr>
            <td>
                @Html.EditorFor(modelItem => item[i].Name)
            </td>
            <td>
                @Html.EditorFor(modelItem => item[i].PreName)
            </td>
            <td>
                @Html.EnumDropDownListFor(modelItem => item[i].Reason, "Present", new { @class = "form-control" })
            </td>
        </tr>
    }

    </table>

    <div class="form-horizontal form-details">
        <input type="submit" value="Save" class="btn btn-default" />
    </div>
</form>

请注意,如果要允许用户输入,则需要将所有DisplayFor更改为EditorFor

答案 1 :(得分:0)

您的状态方法返回的对象不存在(列表)。 如果要在post方法中使用它,则必须返回视图模型。

[HttpGet]
public ActionResult Presence(int id)
{
  List<PresenceVM> model = context.Players.Select(u => new PresenceVM
                           {
                             PlayerId = s.Id,
                             Name = s.Name,
                             PreName = s.PreName,
                             ActivityId = id
                           }).ToList();

   return View(model);
}

答案 2 :(得分:0)

您似乎不使用Form tag

查看

@model MyViewModel
@using (Html.BeginForm(("Presence"))
{
    @Html.AntiForgeryToken()

    //divs

    <div class="form-horizontal form-details">
        <input type="submit" value="Save" class="btn btn-default" />
    </div>
}

最好传递一个Model比一个Model列表

VIEWMODEL

public class MyViewModel{
    public IList<PresenceVM> MyList {get;set;}
}

CONTROLLER

public ActionResult Presence(MyViewModel xxx)
{
    //whatever
}