2014-04-02 63 views
0

如何在我的控制器中设置一个值,使我的视图将该值更新到数据库中?如何将值传递给控制器​​中的CREATE函数?

我的控制器功能:

public ActionResult Create([Bind(Prefix = "id")]int testid) 
    { 

     if (testid != 0) 
     { 
     ViewBag.sectionid = new SelectList(db.section, "id", "name"); 

     ViewBag.testid =new SelectList(db.test.Where(t => t.id == testid), "id", "name"); 

      return View(); 
     } 
     else 
     { 
      return View("mislukt"); 
     } 
    } 

    // 
    // POST: /TestSection/Create 

    [HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult Create(testsection testsection) 
    { 
     if (ModelState.IsValid) 
     { 
      db.testsection.Add(testsection); 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 

     ViewBag.sectionid = new SelectList(db.section, "id", "name", testsection.sectionid); 
     ViewBag.testid = new SelectList(db.test, "id", "name", testsection.testid); 
     return View(testsection); 
    } 

我的观点: testsection

 <div class="editor-label"> 
      @Html.LabelFor(model => model.testid, "test") 
     </div> 
     <div class="editor-field"> 
      @Html.DropDownList("testid", String.Empty) 
      @Html.ValidationMessageFor(model => model.testid) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.sectionid, "section") 
     </div> 
     <div class="editor-field"> 
      @Html.DropDownList("sectionid", String.Empty) 
      @Html.ValidationMessageFor(model => model.sectionid) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.mindifficulty) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.mindifficulty) 
      @Html.ValidationMessageFor(model => model.mindifficulty) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.maxdifficulty) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.maxdifficulty) 
      @Html.ValidationMessageFor(model => model.maxdifficulty) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.questioncount) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.questioncount) 
      @Html.ValidationMessageFor(model => model.questioncount) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.weight) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.weight) 
      @Html.ValidationMessageFor(model => model.weight) 
     </div> 

     <p> 
      <input type="submit" value="Create" /> 
     </p> 
    </fieldset> 

这个工作,但给了我这样的:

not good

,我想这一点:

want this

我尝试了使用:

ViewBag.testid = db.test.Where(t => t.id == testid); 

,然后就设置这样的观点:

<div class="editor-field"> 
      @ViewBag.testid 
      @Html.ValidationMessageFor(model => model.testid) 
     </div> 

但我得到一个FK约束当我这样做。

有人知道我能得到我想要的结果吗?

回答

0

如果我得到你正确的....尝试与textboxFor并使其只读。

+0

那么我该如何将ViewBag链接到它呢? – kicked11

+0

您将不会使用ViewBag,而是将值分配给您的模型中的某个属性,并使用与该属性关联的TextBoxFor – CSharper

0

我发现了一个解决方案,虽然它非常糟糕!所以如果有人知道更好的解决方案,请让我知道!

我现在所做的是将dropdonwlist的选定值设置为1(因为where-clause总是正确的值,所以我只获得1个选项),然后使用javascript隐藏我的视图中的下拉列表。

正如我所说的非常糟糕的解决方案,所以请如果你知道我可以如何解决它适当让我知道。

只是要清楚我想从另一个视图传递testid到createview(如图所示),然后将输入的值与该testid一起写入我的数据库!

相关问题