我的Overloader网址最后包含数字id
。
我目前在代码中使用id
做了很多不同的事情。
但是现在我想将它传递给数据库时将其设置为SurveyID
值。
我在查看ViewBag
和Binds
中包含的不同字符串时遇到了一些困难。
这是我的Controller
代码:
public ActionResult Create(int? id)
{
//GET QUESTIONS FROM DATABASEDEPENDING ON SURVEYID
var survey = db.Surveys.FirstOrDefault(s => s.SurveyID == id);
if (survey != null)
{
ViewBag._CurrentSurveyID = id;
//DOING STUFF
}
else if (survey == null) {
//DOING STUFF
}
////ATTEMPT
//string currentSurveyID = ViewBag._CurrentSurveyID;
//ViewBag.SurveyID = new SelectList(db.Surveys, currentSurveyID, "SurveyName");
//STANDARD CODE
ViewBag.SurveyID = new SelectList(db.Surveys, "SurveyID", "SurveyName");
return View();
}
我可以在加载Create()页面时使用id
值获得很多成功。 (上图)。
但是我需要帮助在Create()数据库输入时包含id
值。 (下面)。
Bind中的字符串值来自哪里?观点,我假设?
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "IntroResponseID,SurveyID,IntroAnswer1,IntroAnswer2,IntroAnswer3,IntroAnswer4,IntroAnswer5,IntroFreeText,IntroTime")] IntroResponse introResponse)
{
if (ModelState.IsValid)
{
db.IntroResponses.Add(introResponse);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.SurveyID = new SelectList(db.Surveys, "SurveyID", "SurveyName", introResponse.SurveyID);
return View(introResponse);
}
将id
作为SurveyID
输入包含的正确方法是什么?
已获取更多信息:
View
:
如果id
在网址中,则代码会进入第一个if。如果网址中没有id
;显示下拉列表以选择SurveyID。
请注意,Dropdown可以完美地选择SurveyID。
但是,正如您可以看到尝试将SurveyID设置为隐藏@ActionLink
中的ViewBag代码,这会失败。
@{
if (ViewBag.vbgSenderContactName != null || ViewBag.vbgSenderCompanyName != null || ViewBag.vbgResponderContactName != null || ViewBag.CounsultantName != null)
{
string _stringSurveyID = @ViewBag._CurrentSurveyID.ToString();
@ViewBag._CurrentSurveyID;
<div class="hidden">
@Html.ActionLink("SurveyID", "", new { value = "_stringSurveyID" })
</div>
}
else
{
<div class="form-group">
@Html.LabelFor(model => model.SurveyID, "Välj undersökning", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("SurveyID", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.SurveyID, "", new { @class = "text-danger" })
</div>
</div>
}
}
Model
财产:
[Required()]
public int SurveyID { get; set; }
[ForeignKey("SurveyID")]
[Display(Name = "Välj undersökning")]
public virtual Survey Survey { get; set; }
答案 0 :(得分:1)
如果要将值传递给ActionLink
,则需要在操作方法中使用确切的属性名称。
@Html.ActionLink("SurveyID", "", new { id= ViewBag._CurrentSurveyID})
修正小错字
答案 1 :(得分:0)
感谢 Vsevolod Goloviznin
工作代码:
<强>控制器:强>
public ActionResult Create(int? id)
{
//GET QUESTIONS FROM DATABASEDEPENDING ON SURVEYID
var survey = db.Surveys.FirstOrDefault(s => s.SurveyID == id);
if (survey != null)
{
string stringID = id.ToString();
ViewBag._currentSurveyID = int.Parse(stringID);
查看:强>
if (ViewBag.vbgSenderContactName != null || ViewBag.vbgSenderCompanyName != null || ViewBag.vbgResponderContactName != null || ViewBag.CounsultantName != null)
{
<div class="hidden">
@Html.LabelFor(model => model.SurveyID, "Välj undersökning", htmlAttributes: new { id = ViewBag._currentSurveyID })
<div class="col-md-10">
@Html.DropDownList("SurveyID", null, htmlAttributes: new { id = ViewBag._currentSurveyID })
@Html.ValidationMessageFor(model => model.SurveyID, "", new { id = ViewBag._currentSurveyID })
</div>
</div>
}
else if (1 > 2)
{
<div class="form-group">
@Html.LabelFor(model => model.SurveyID, "Välj undersökning", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("SurveyID", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.SurveyID, "", new { @class = "text-danger" })
</div>
</div>
}
我认为我的ActionLink无法折叠从SurvedID
派生的所有属性。
出于这个原因,我确实使用了“标准”DropDown。但隐藏。
请注意,我强制ViewBag._currentSurveyID
成为Controller中的int