请原谅我,因为我正在学习MVC3。
我有2个数据库表,一个用于比赛,一个用于答案。对于每个比赛,将有3个与之相关的答案。
我的创建视图是我的竞赛模型的强类型。我想要做的是向该视图添加3个未绑定的输入框并在控制器中检索结果
我的控制器看起来像这样
Function Create(competition As Competition) As ActionResult
If ModelState.IsValid Then
db.Competitions.AddObject(competition)
db.SaveChanges()
Return RedirectToAction("Index")
End If
ViewBag.ClientID = New SelectList(db.Clients, "ClientID", "ClientName", competition.ClientID)
Return View(competition)
End Function
我想做的是这样的事情
Function Create(competition As Competition) As ActionResult
If ModelState.IsValid Then
'DIM ANSWERA AS STRING = INPUTA
'DIM ANSWERB AS STRING = INPUTB
'DIM ANSWERC AS STRING = INPUTC
db.Competitions.AddObject(competition)
db.SaveChanges()
'SAVE ANSWERS TO ANSWER TABLE
Return RedirectToAction("Index")
End If
ViewBag.ClientID = New SelectList(db.Clients, "ClientID", "ClientName", competition.ClientID)
Return View(competition)
End Function
很抱歉,如果我没有正确格式化问题。希望你能得到我想要做的事情的要点
感谢
我现在已经完成了上述工作,我希望能够在视图中编辑竞赛详情
Function Edit(id As Integer) As ViewResult
Dim competition As Competition = db.Competitions.Single(Function(c) c.CompetitionID = id)
ViewBag.ClientID = New SelectList(db.Clients, "ClientID", "ClientName", competition.ClientID)
Return View(competition)
End Function
所以我支持视图返回我的比赛细节但是如何添加3个输入框来显示与该比赛相关的3个答案?
答案 0 :(得分:1)
Request("NAME")
将查找NAME作为表单元素,cookie,查询字符串参数或服务器变量的名称。
Dim answerA as String = Request("answerA")
Dim answerB as String = Request("answerB")
Dim answerC as String = Request("answerC")
为了安全起见,您应该使用以下方法专门定位名为“answerA”的表单元素:
Dim answerA as String = Request.Form("answerA")
但上述两个例子中的任何一个都可以使用。
答案 1 :(得分:0)
如果控制器参数名称与输入名称匹配,MVC将为您执行绑定。所以如果你的html看起来像:
<input type="text" name="answerA" />
<input type="text" name="answerB" />
<input type="text" name="answerC" />
然后你的控制器可以简单地
Function Create(competition As Competition, answerA As string, answerB As string, answerC As string) As ActionResult
或者,您可以使用名为answers
的数组,并使用answers[0]
,answers[1]
等名称声明输入字段...