2013-01-07 57 views
0

我正在寫一個MVC3項目,顯示創建事件時要選擇的用戶主題。但是,每當從下拉列表中選擇一個值時,字段「主題」的值始終爲空。DropDownList總是發佈空字段

這裏是類:

public class VolunteerEvent 
{ 
     public int VolunteerEventID { get; set; } 
     public DateTime VolunteerDate { get; set; } 
     public int ZIPcode { get; set; } 
     public int Hours { get; set; } 
     public string Topic { get; set; } 
} 

啞類存儲串

public class VolunteerTopic 
{ 
    public int VolunteerTopicID { get; set; } 
    public string VolunteerTopicName { get; set; } 
} 

控制器,用於創建操作

public ActionResult Create() 
    { 
     ViewBag.VolunteerTopicID = new SelectList(db.VolunteerTopics, "VolunteerTopicID", "VolunteerTopicName"); 
     return View(); 
    } 

// 
// POST: /LogVolunteerEvent/Create 
[HttpPost] 
public ActionResult Create(VolunteerEvent volunteerevent) 
{ 

    if (ModelState.IsValid) 
    { 
     db.VolunteerEvents.Add(volunteerevent); 
     db.SaveChanges(); 
     return RedirectToAction("Index"); 
    } 
    ViewBag.VolunteerTopics = new SelectList(db.VolunteerTopics, "VolunteerTopicID", "VolunteerTopicName", 
     volunteerevent.Topic); 

    return View(volunteerevent); 
} 

和視圖的相關部分

<div class="editor-label"> 
      @Html.LabelFor(model => model.Topic) 
     </div> 
     <div class="editor-field"> 
      @Html.DropDownList("VolunteerTopicID") 
      @Html.ValidationMessageFor(model => model.Topic) 
     </div> 

對於那些有興趣

<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="utf-8" /> 
    <title>Create</title> 
    <link href="/Content/Site.css" rel="stylesheet" type="text/css" /> 
    <script src="/Scripts/jquery-1.7.1.min.js" type="text/javascript"></script> 
    <script src="/Scripts/modernizr-2.5.3.js" type="text/javascript"></script> 
</head> 
<body> 
    <div class="page"> 

<h2>Create</h2> 

<script src="/Scripts/jquery.validate.min.js" type="text/javascript"></script> 
<script src="/Scripts/jquery.validate.unobtrusive.min.js" type="text/javascript"></script> 

<form action="/logvolunteerEvent/Create" method="post"> <fieldset> 
     <legend>VolunteerEvent</legend> 

     <div class="editor-label"> 
      <label for="VolunteerDate">VolunteerDate</label> 
     </div> 
     <div class="editor-field"> 
      <input class="text-box single-line" data-val="true" data-val-required="The VolunteerDate field is required." id="VolunteerDate" name="VolunteerDate" type="text" value="" /> 
      <span class="field-validation-valid" data-valmsg-for="VolunteerDate" data-valmsg-replace="true"></span> 
     </div> 

     <div class="editor-label"> 
      <label for="ZIPcode">ZIPcode</label> 
     </div> 
     <div class="editor-field"> 
      <input class="text-box single-line" data-val="true" data-val-number="The field ZIPcode must be a number." data-val-required="The ZIPcode field is required." id="ZIPcode" name="ZIPcode" type="text" value="" /> 
      <span class="field-validation-valid" data-valmsg-for="ZIPcode" data-valmsg-replace="true"></span> 
     </div> 

     <div class="editor-label"> 
      <label for="Hours">Hours</label> 
     </div> 
     <div class="editor-field"> 
      <input class="text-box single-line" data-val="true" data-val-number="The field Hours must be a number." data-val-required="The Hours field is required." id="Hours" name="Hours" type="text" value="" /> 
      <span class="field-validation-valid" data-valmsg-for="Hours" data-valmsg-replace="true"></span> 
     </div> 

     <div class="editor-label"> 
      <label for="Topic">Topic</label> 
     </div> 
     <div class="editor-field"> 
      <select id="VolunteerTopicID" name="VolunteerTopicID"> 
<option value="1">  Advocacy</option> 
...a bunch of values... 
</select> 
      <span class="field-validation-valid" data-valmsg-for="Topic" data-valmsg-replace="true"></span> 
     </div> 

     <p> 
      <input type="submit" value="Create" /> 
     </p> 
    </fieldset> 
</form> 
<div> 
    <a href="/logvolunteerEvent">Back to List</a> 
</div> 

     </section> 
     <footer> 
     </footer> 
    </div> 
</body> 
</html> 
+0

請發表您的HTML – Madbreaks

+0

我剛剛發佈的其餘部分進行訪問查看 – anguyen

+1

不,請發佈您的* HTML * – Madbreaks

回答

0

我覺得你把事情錯了HTML結果,這是你應該做的方式:

型號:

public class VolunteerEvent 
{ 
    // other fields 
    public int TopicId { get; set; } 
} 

查看:

@model VolunteerEvent 

(inside some form) 
@Html.DropDownListFor(m => m.TopicId, new SelectList(aListOfTopics, "Id", "Name")) 

(這是對於人IST的Topic對象其列IdName

控制器:

[HttpPost] 
public void Create(VolunteerEvent volunteerEvent, FormCollection collection) 
{ 
    var topicId = volunteerEvent.TopicId; 
    // do something 
    // you can also get the value from collection["TopicId"] 
} 
+0

問題是我認爲'VolunteerEvent's有一個與它們相關的字符串。不是'topicID' – anguyen

+0

所以,你是用一個字符串來識別主題? – lante

0

這裏的問題是,你的Dropdown列表勢必VoluteerTopicId,,但不是要傳遞到模型的一部分,你的發佈行動。

這裏最快的修復得到它的工作是創建一個視圖模型:

public class VolunteerViewModel 
{ 
    public VolunteerEvent VolunteerEvent {get; set;} 
    public VolunteerTopic {get; set;} 
} 

現在強烈鍵入您的視圖這種觀點模型而不是VolunteerEvent,並相應地重構。

那麼你的職位採取行動的簽名更改爲public ActionResult Create(VolunteerViewModel viewModel)

現在你的清單應綁定到模型,選擇的值可以在viewModel.VolunteerTopic.VolunteerTopicId

相關問題