C# MVC3将模型传递给控制器-接收空值

C# MVC3将模型传递给控制器-接收空值,c#,asp.net,asp.net-mvc-3,C#,Asp.net,Asp.net Mvc 3,我几周前才开始使用MVC3,在编程方面我还很年轻,现在我仍在学习很多东西。我最近一直在使用模型,使用TextBoxFor和其他帮助程序来填充我的“角色”模型的属性 本质上,我试图做的是定义一个模型,然后将其传递给我的控制器,然而,我在模型中定义为静态值的任何属性在运行时都将作为空值传递 下面是理解发生了什么所需的部分片段 Character.cs - Model // Instances of manipulated objects. otReal db = new otRea

我几周前才开始使用MVC3,在编程方面我还很年轻,现在我仍在学习很多东西。我最近一直在使用模型,使用TextBoxFor和其他帮助程序来填充我的“角色”模型的属性

本质上,我试图做的是定义一个模型,然后将其传递给我的控制器,然而,我在模型中定义为静态值的任何属性在运行时都将作为空值传递

下面是理解发生了什么所需的部分片段

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>
Index.cshtml-查看
@使用(Ajax.BeginForm(“Submit”,新的AjaxOptions{OnComplete=“done”}))
{
@Html.ValidationSummary(true)
新字符信息
@LabelFor(model=>model.Name,“字符名”)
@EditorFor(model=>model.Name)
@Html.ValidationMessageFor(model=>model.Name)
@LabelFor(model=>model.TownList)
@*@Html.DropDownList(“城镇列表”,ViewData[“城镇列表”]作为SelectList)*@
@Html.DropDownListFor(model=>model.TownList,ViewData[“TownList”]作为SelectList)
@Html.ValidationMessageFor(model=>model.TownList)
@LabelFor(model=>model.Sex)
@Html.DropDownListFor(model=>model.Sex,ViewData[“Sex”]作为SelectList)
@Html.ValidationMessageFor(model=>model.Sex)
@LabelFor(model=>model.activity)
@Html.DropDownList(“VocList”,ViewData[“VocList”]作为SelectList)
@Html.ValidationMessageFor(model=>model.Vocation)

}


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

假设您尝试在“newPlayer”上设置值,则可以替换此值

public player newPlayer = new player();
用这个

public player newPlayer { get; set; }

默认的模型绑定器将创建从post back表单开始的所有内容——不需要在模型中实例化它。

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

您可能还需要向我们展示一些视图代码。特别是发布到CharacterController.Submit()的表单。您是否可以提供更多信息,您到底想做什么,您遇到了什么错误,以及该错误发生在哪一行代码中?我得到的错误是“conditions”不能为null,因为它在SQL中是不可为null的列。然而,您看到的上述属性是我拥有的每个属性的基本设置,我几乎可以静态设置它,或者根据从视图传递的“值”设置它。我非常喜欢这个想法背后的概念Atadeini,然而,我不完全确定如何用我当前的属性设置它,因为我正在设置一个实例化对象的值,上面的例子对我来说有点模糊。我很想了解更多,因为这正是我需要的。啊,我想我现在明白你的问题了。我建议对用户可以编辑的数据部分使用单独的视图模型,然后在持久化到数据库之前将其与任何共享数据相结合。你最终会得到更多的类,但从长远来看,它会让事情变得更干净。在方法上。祝你好运。是的,我可能会采取这种方法。作为一个临时修复,只是为了用MVC进行更多测试,我刚刚用一个构造函数设置了角色,该构造函数在创建对象时设置所有这些值。