2012-10-04 175 views
0

我試圖從DropDownList中獲取信息並將SelectListItem「Value」發佈到控制器中的另一個ActionResult方法。它將傳遞給它的控制器將獲取一個整數值並在另一個查詢中使用它。用於填充的DropDownList是如下將DropDownList值發佈到MVC控制器

我控制器的方法:

public ActionResult SelectCategory() 
{ 
    var model = new TestTypesViewModel(); 

    var query = (from ab in db.Tbl_Admin_Batch 
       from ub in db.Tbl_Admin_User_Batch 
       where ub.User_Id == 45875 && ab.Batch_Id == ub.Batch_Id 
       select ab).ToList(); 

    model.Test_Types = query.Select(c => new SelectListItem 
     { 
      Text = c.Batch_Name, 
      Value = c.Batch_Id.ToString() 
     }).ToList(); 

    return View(model); 

我的視圖模型爲TestTypesViewModel如下:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 

namespace HFI_Assessment_Administration.ViewModels 
{ 
    public class TestTypesViewModel 
    { 
     public int Batch_ID { get; set; } 
     public string Test_Type { get; set; } 

     public IEnumerable<SelectListItem> Test_Types { get; set; } 
    } 
} 

我是新來的MVC,並試圖讓事情變得簡單,我知道Batch_ID和Test_Type沒有被指定,但我不確定他們在這一點上是否有必要。

任何意見或幫助將不勝感激,非常感謝!

編輯:

我現在有選擇分類視圖如下:

@model HFI_Assessment_Administration.ViewModels.TestTypesViewModel 

@{ 
    ViewBag.Title = "SelectCategory"; 
} 

@using (Html.BeginForm("Practice", "WebFormUserList")) 
{ 
    @Html.DropDownListFor(x => x.Batch_ID, Model.Test_Types) 
    <input type="submit" /> 
} 

它被傳遞的控制器如下:

[HttpPost] 
public ActionResult Practice(TestTypesViewModel model, int Parent_ID = 45875) 
{ 
    var query = (from u in db.Users 
       join ur in db.User_Relationship on u.User_ID equals ur.Child_ID 
       join ub in db.Tbl_Admin_User_Batch on u.User_ID equals ub.User_Id 

       join ut in db.User_Tests on u.User_ID equals ut.User_ID into ps 
       from ut in ps.DefaultIfEmpty() 

       join lu in db.Lookups on u.First_LanguageID equals lu.LookupID 

       where ur.Parent_ID == Parent_ID && ub.Batch_Id == model.Batch_ID 

       group new { u, lu, ut } by new 
       { 
        u.User_ID, 
        u.Forename, 
        u.Surname, 
        u.Client_Code, 
        u.User_Name, 
        u.Password, 
        u.Email, 
        u.Gender, 
        u.Report_date, 
        u.EmailDate, 
        u.Job_Function, 
        lu.LookupValue 
       } into g 

       select new UserViewModel 
        { 
         User_ID = g.Key.User_ID, 
         Forename = g.Key.Forename, 
         Surname = g.Key.Surname, 
         Client_Code = g.Key.Client_Code, 
         User_Name = g.Key.User_Name, 
         Password = g.Key.Password, 
         Email = g.Key.Email, 
         Gender = g.Key.Gender, 
         Report_Date = g.Key.Report_date, 
         Email_Date = g.Key.EmailDate, 
         Test_Count = g.Count(p => p.ut.Test_ID != null), 
         Test_Completed = g.Count(p => p.ut.Completed != null), 
         Job_Function = g.Key.Job_Function, 
         Lookup_Value = g.Key.LookupValue 
        }).ToList(); 

     return View(query); 
    } 

的信息查看實踐如下:

@model IEnumerable<HFI_Assessment_Administration.ViewModels.UserViewModel> 

@{ 
    ViewBag.Title = "ChildUsers"; 
} 

<h2>Practice</h2> 

<div> 
    @{ 
     var grid = new WebGrid(Model); 
    } 

    @grid.GetHtml(

    tableStyle: "webgrid", 
    headerStyle: "webgrid-header", 
    footerStyle: "webgrid-footer", 
    alternatingRowStyle: "webgrid-alternating-row", 
    selectedRowStyle: "webgrid-selected-row", 
    rowStyle: "webgrid-row-style", 

    columns: grid.Columns 
    (
     grid.Column(columnName:"User_ID", header: "User ID", style: "text-align-center"), 
     grid.Column(columnName:"Forename", header: "Forename", style: "text-align-center"), 
     grid.Column(columnName:"Surname", header: "Surname", style: "text-align-center"), 
     grid.Column(columnName:"Client_Code", header: "Client Code", style: "text-align-center"), 
     grid.Column(columnName:"User_Name", header: "User Name", style: "text-align-center"), 
     grid.Column(columnName:"Password", header: "Password", style: "text-align-center"), 
     grid.Column(columnName:"Email", header: "Email", style: "text-align-center"), 
     grid.Column(columnName:"Gender", header: "Gender", style: "text-align-center"), 
     grid.Column(columnName:"Report_Date", header: "Report Date", style: "text-align-center"), 
     grid.Column(columnName:"Email_Date", header: "Email Date", style: "text-align-center"), 
     grid.Column(columnName:"Test_Count", header: "Count", style: "text-align-center"), 
     grid.Column(columnName:"Test_Completed", header: "Completed", style: "text-align-center"), 
     grid.Column(columnName:"Job_Function", header: "Job Function", style: "text-align-center"), 
     grid.Column(columnName:"Lookup_Value", header: "Language", style: "text-align-center") 
     )   
    ) 
</div> 

一切都很好,直到我嘗試去網格的下一頁或嘗試排序網格。在「/」應用程序中出現服務器錯誤時,出現錯誤。無法找到該資源。

回答

0

有很多方法可以做到這一點。您可以使用標準<form>標籤或使用AJAX發送該值。

讓我們來看看第一種情況:

@model TestTypesViewModel 
@using (Html.BeginForm("SomeAction", "SomeController")) 
{ 
    @Html.DropDownListFor(x => x.Test_Type, Model.Test_Types) 
    <button type="submit">OK</button> 
} 
在你的目標行動

現在:

[HttpPost] 
public ActionResult SomeAction(TestTypesViewModel model) 
{ 
    // model.Test_Type will contain the selected value here 

    // Notice that if you intend to return the same view as the GET action 
    // (SelectCategory.cshtml) you should assign the Test_Types property on 
    // your model by querying your database the same way you did in the GET action 
    // before passing this model to the view. If on the other hand you intend to 
    // redirect here you don't need to assign it. 
} 

第二種可能性是使用AJAX。所以,你可以如給您下拉列表的ID,並有一些鏈接,點擊時會調用目標控制器動作發送它使用AJAX所選值:

@Html.DropDownListFor(x => x.Test_Type, Model.Test_Types, new { id = "testTypeDdl" }) 
@Html.ActionLink("click me", "SomeAction", null, new { id = "myLink" }) 

,然後當一些按鈕或鏈接被點擊使用$.ajax要求:

$(function() { 
    $('#myLink').click(function() { 
     $.ajax({ 
      url: this.href, 
      type: 'GET', 
      cache: false, 
      data: { selectedValue: $('#testTypeDdl').val() }, 
      success: function(result) { 
       alert('The value was submitted to the server'); 
      } 
     }); 
     return false; 
    }); 
}); 

,現在你的控制器動作可能具有以下特徵:

public ActionResult SomeAction(string selectedValue) 
{ 
    // Process the selected value here and return some result. 
    // This result could either be a PartialView or a JsonResult 
    // depending on your requirements. 
} 
+0

哇,感謝您的快速響應,我會給予一個現在是一個去d看看我在哪裏得到它! – user1688784

+0

一個簡單的問題,您的評論意味着什麼://注意,如果您打算返回與GET操作相同的視圖 //(SelectCategory。cshtml),您應該在 //您的模型上指定Test_Types屬性,方法是在將此模型傳遞到視圖之前,以與GET動作 中相同的方式查詢數據庫。另一方面,如果您打算在這裏重定向 ,則不需要分配它。 – user1688784

+0

我的意思是說,如果你從POST動作返回View(model);你需要給'model.Test_Types = ...'賦值,就像你在GET動作中一樣。 –

相關問題