【问题标题】:MVC3 Passing Model to Controller - Receiving Null valuesMVC3 将模型传递给控制器​​ - 接收空值
【发布时间】:2011-04-17 22:30:27
【问题描述】:

几周前我刚开始使用 MVC3,在编程阶梯上还很年轻,我还在学习很多东西。我最近一直在使用模型,使用 TextBoxFor 和其他助手来填充我的“字符”模型的属性。

基本上我想要做的是定义一个模型,然后将它传递给我的控制器,但是我在模型中定义为静态值的任何属性都在运行时作为空值传递。

以下是了解发生了什么所需的部分 sn-ps..

Character.cs - Model

    // Instances of manipulated objects.
    otReal db = new otReal();

    public player newPlayer = new player();

    public byte[] conditions
    {
        get
        {
            return newPlayer.conditions;
        }
        set
        {
            byte[] array1 = null;

            array1 = new byte[16 * 12 * 3];            

            newPlayer.conditions = array1;
        }
    }

CharacterController.cs

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Submit(Character c)
    {
        // Add a new character to the database via LINQ to Entities.         
        otReal.AddToplayers(c.newPlayer);
        otReal.players.AddObject(c.newPlayer);
        otReal.SaveChanges();

        return View();
    }

我的视图中唯一的助手是用户实际需要与之交互的助手。如果我进入我的控制器并在那里设置值,它们将被设置为正确的值并插入。任何帮助将不胜感激!

Index.cshtml - View
@using (Ajax.BeginForm("Submit", new AjaxOptions { OnComplete = "done" }))
{
@Html.ValidationSummary(true)
<fieldset>
    <legend>New Character Information</legend>
    <div class="editor-label">
        @Html.LabelFor(model => model.Name, "Character Name")
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Name)
        @Html.ValidationMessageFor(model => model.Name)
    </div>
    <div class="editor-label">
        @Html.LabelFor(model => model.TownList)
    </div>
    <div class="editor-field">
        @* @Html.DropDownList("TownList", ViewData["TownList"] as SelectList)*@
        @Html.DropDownListFor(model => model.TownList, ViewData["TownList"] as SelectList)
        @Html.ValidationMessageFor(model => model.TownList)
    </div>
    <div class="editor-label">
        @Html.LabelFor(model => model.Sex)
    </div>
    <div class="editor-field">
        @Html.DropDownListFor(model => model.Sex, ViewData["sex"] as SelectList)
        @Html.ValidationMessageFor(model => model.Sex)
    </div>
    <div class="editor-label">
        @Html.LabelFor(model => model.Vocation)
    </div>
    <div class="editor-field">
        @Html.DropDownList("VocList", ViewData["VocList"] as SelectList)
        @Html.ValidationMessageFor(model => model.Vocation)
    </div>
    <p>
        <input id="button" type="submit" value="Create" />
    </p>
    <div style="font-family: Tahoma; font-size: large" id="completeDiv" style="display: none;">
    </div>
    <span></span>
</fieldset>

}

基本上我在这里要做的是创建我的模型,该模型具有数据库表中每个“字符”都将具有的一堆基本值。这样,当玩家创建他/她的角色时,他/她将能够输入角色名称、性别、职业(职业)、起始城镇和所有其他值将在幕后填充并传递给控制器​​进行创建。

【问题讨论】:

  • 您可能还需要向我们展示一些查看代码。特别是 POST 到 CharacterController.Submit() 的表单
  • 您能否提供更多信息,您到底想做什么,遇到什么错误以及该错误发生在哪一行代码上?
  • 我得到的错误是“条件”不能为空,因为它是 SQL 中的 NOT NULLABLE 列。但是,您看到的上述属性是我拥有的每个属性的基本设置,我几乎要么静态设置它,要么根据从视图传递的“值”设置它。

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


【解决方案1】:

假设您尝试在“newPlayer”上设置值,那么您可以替换它

public player newPlayer = new player();

有了这个

public player newPlayer { get; set; }

默认模型绑定器将在回发时从表单创建所有内容——无需在模型中实例化它。

【讨论】:

  • 我真的很喜欢 ataddeini 这个想法背后的概念,但是我不完全确定如何使用我当前的属性来设置它,因为我正在设置实例化对象的值,以了解上述示例的位置对我来说有点模糊。我很想进一步了解它,因为它似乎正是我所需要的。
  • 啊,我想我现在明白你的问题了。我建议对用户可以编辑的数据部分使用单独的视图模型,然后在持久保存到数据库之前将其与任何共享数据结合起来。你最终会得到更多的课程,但从长远来看,它会让事情变得更干净。 Here's some more information 正在接近。祝你好运。
  • 是的,我可能会采用这种方法。作为一个临时修复,只是为了使用 MVC 进行更多测试,我刚刚使用构造函数设置了 Character,该构造函数在对象创建时设置所有这些值。
【解决方案2】:

为我的模型创建了一个构造函数,并在构造函数中通过新实例化的播放器模型设置了默认值。临时修复,直到我阅读 ataddeini 的解决方案。谢谢大家!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-21
    • 1970-01-01
    • 2015-11-22
    • 1970-01-01
    • 2011-07-03
    相关资源
    最近更新 更多