几个星期前我刚开始使用MVC3,并在编程阶段很年轻,但我仍然学习很多。我最近一直在使用模型,使用TextBoxFor和其他助手来填充我的“角色”模型的属性。MVC3将模型传递给控制器 - 接收空值
本质上我想要做的是定义一个模型,然后将它传递给我的控制器,但是,在我的模型中定义为静态值的任何属性都将作为运行时的空值传递。
下面是要了解什么是继续下去所需要的部分的一些片断..
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的形式() – 2011-04-17 23:28:45
您能否提供更多信息,您正在尝试做什么,发生了哪些错误以及发生错误的代码行? – 2011-04-17 23:29:25
我得到的错误是“条件”不能为空,因为它是SQL中的NOT NULLABLE列。然而,您看到的上述属性是我拥有的每个属性的基本设置,我几乎都是静态设置它或基于从视图传递的“值”进行设置。 – jhartzell 2011-04-18 01:38:55