将URL重载到Create()方法中

时间:2014-12-08 09:50:27

标签: asp.net-mvc url controller

我的Overloader网址最后包含数字id。 我目前在代码中使用id做了很多不同的事情。

但是现在我想将它传递给数据库时将其设置为SurveyID值。 我在查看ViewBagBinds中包含的不同字符串时遇到了一些困难。

这是我的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; }

2 个答案:

答案 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