简体   繁体   English

asp.net mvc 5-将ViewBag项传递回控制器

[英]asp.net mvc 5 - Passing a ViewBag item back to the controller

I am trying to pass a ViewBag item from my view back into the post method for that view but I am having difficulty. 我试图将ViewBag项目从我的视图传递回该视图的post方法,但遇到了困难。

This is a simplified version of my methods: 这是我的方法的简化版本:

public ActionResult CreateGame(int id)
{
    var selPlayer = db.Players.Find(id);

    if (selPlayer == null)
    {
        return HttpNotFound();
    }

    ViewBag.SelPlayerId = selPlayer.PlayerID;

    PlayerGame newGame = new PlayerGame ();

    return View(newGame);
}

[HttpPost]
public ActionResult CreateGame(PlayerGame newGame)
{
    newGame.GameTitle = newGame.GameTitle;
    newGame.GameNotes = newGame.GameNotes;
    newGame.PlayerID = newGame.PlayerID;

    return RedirectToAction("PlayerView");
    return View(newGame);
}

This is a simplified version of my View: 这是我的视图的简化版本:

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    <div class="form-horizontal">
        <fieldset>

            <div class="editor-label">
            @Html.LabelFor(model => model.GameTitle)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.GameTitle)
        </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.GameNotes)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.GameNotes)
            </div>

            @Html.HiddenFor(model => model.PlayerID, new { PlayerID = ViewBag.SelPlayerId })
            <p>
                <input type="submit" value="Submit Game" class="btn btn-info" />
            </p>
        </fieldset>
        </div>
        }

In the POST method you can see I am trying to set the PlayerID equal to the player Id that I am passing through as ViewBag.SelPlayerID but when I look at the values that are being passed through in the debugger the id is always equal to 0 which is not right as the player I am using to test has an id of 1. 在POST方法中,您可以看到我正在尝试将PlayerID设置为等于我作为ViewBag.SelPlayerID传递的播放器ID,但是当我查看在调试器中传递的值时,ID始终等于0这不正确,因为我要测试的播放器的ID为1。

My question is how can I set the player ID for the new game I am creating equal to the player ID I am passing through as the ViewBag item, or alternatively if there is an easier way of doing this that I have overlooked. 我的问题是,如何将正在创建的新游戏的玩家ID设置为与我通过的玩家ID相同,作为ViewBag项,或者是否有一种更简单的方法被我忽略了。

Note: The player id is a foreign key in my Player Game model: 注意:玩家ID是我的玩家游戏模型中的外键:

//Foreign Key for Player
        public int PlayerID { get; set; }

        [ForeignKey("PlayerID")]
        public virtual Player Player { get; set; }

You usage of new { PlayerID = ViewBag.SelPlayerId } in the @Html.HiddenFor() method is setting a html attribute, not the value of the property. 您在@Html.HiddenFor()方法中使用new { PlayerID = ViewBag.SelPlayerId }正在设置html属性,而不是属性的值。

If you inspect the html your generating it will be 如果您检查html,则生成的将是

<input type="hidden" name="PlayerID" PlayerID="1" value="0" />

Change your GET method to set the value of the property in the model (and delete the ViewBag property) 更改您的GET方法以设置模型中属性的值(并删除ViewBag属性)

PlayerGame newGame = new PlayerGame() { PlayerID = selPlayer.PlayerID };
return View(newGame);

and in the view use 并在视图中使用

@Html.HiddenFor(m => m.PlayerID)

This: 这个:

@Html.HiddenFor(model => model.PlayerID, new { PlayerID = ViewBag.SelPlayerId })

Is creating a hidden element, bound to model.PlayerID , but with a custom Html attribute of PlayerID , you are getting 0 as thats the default value for int and you are not setting it on the model. 正在创建绑定到model.PlayerID的隐藏元素,但是使用PlayerID的自定义Html属性,您将获得0因为那是int的默认值,并且未在模型上进行设置。

Based on your (simplified) code sample, you should be able to set the model PlayerID to your selected value in the get action on the controller: 基于您的(简化的)代码示例,您应该能够在控制器上的get操作中将模型PlayerID设置为所选值:

newGame.PlayerID = selPlayer.PlayerID

And then 接着

@Html.HiddenFor(m => m.PlayerID)

If for some reason you have to use ViewBag , and can't populate it on the model as above you can use @Html.Hidden instead to ensure the value comes back in. 如果由于某种原因您必须使用ViewBag ,并且不能像上面那样在模型上填充它,则可以使用@Html.Hidden来确保返回该值。

@Html.Hidden("PlayerID", ViewBag.SelPlayerId)

Will result in: 将导致:

<input type="PlayerID" value="1" />

Which should model bind back to the model. 哪个模型应绑定回模型。 If that's not an option and to do it more manually, you can change the first parameter to something like "SelectedPlayerID" and then you can either pull out in from Request in the controller post action. 如果这不是一个选择,而是要手动进行,则可以将第一个参数更改为“ SelectedPlayerID”,然后可以在控制器后操作中从“ Request中退出。

只是试图在CSHTML视图page.On回传此使用viewbag alone.Set隐藏式元素你会得到你的argument.This您的玩家ID将工作,如果你使用的是输入型提交

<input type="hidden" id="PlayerID" name="PlayerID" value="@ViewBag.SelPlayerId"/>

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

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