2016-07-13 54 views
0

我想在我的index.cshtml和welcome.cshtml頁面之間爲性別(gender.cshtml)創建一個下拉列表,我該怎麼做。另一個問題是,當有人在gender.cshtml頁面中選擇他們的性別時,我可以如何讓它顯示在welcome.cshtml頁面中。例如,如果某人選擇了男性,它就會顯示爲WELCOME「在索引文本框中輸入的姓名」,他是男性。C#MVC DropDownList

HomeController.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 
using System.Data.Entity; 
using Newproject.Models; 

namespace Newproject.Controllers 
{ 
public class HomeController : Controller 
{ 
    public ActionResult Index() 
    { 
     return View(); 
    } 
    public ActionResult Gender() 
    { 
     return View(); 
    } 
    public ActionResult Welcome(string SearchString) 
    { 
     ViewBag.Name = SearchString; 
     return View(); 
    } 
    } 
    } 

Index.cshtml

@{ 

ViewBag.Title = "Welcome"; 

} 

    <br /> 
    @Html.Label("Name") 
    <br /> 
    @using (Html.BeginForm("Welcome", "Home", FormMethod.Post)) 
    { 
     @Html.TextBox("SearchString"); 
<input id="btnSubmit" name="btnSubmit" placeholder="test" type="submit" value="Search" /> 
    } 

USER.cs

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

namespace Newproject.Models 
{ 
public class User 
{ 
    public string SearchString { get; set; } 
} 
} 

Gender.cs

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

namespace Newproject.Models 
{ 
public class Gender  
{ 
    public string Name { get; set; } 
} 
} 

Gender.cshtml

@{ 
ViewBag.Title = "Gender"; 
} 

<h2>Gender</h2> 

Welcome.cshtml

@{ 
ViewBag.Title = "Welcome"; 
string CustNo = ViewBag.Name; 
} 


    <p>Welcome @CustNo</p> 
+0

這是什麼 「在我index.cshtml和welcome.cshtml頁面之間的」 連是什麼意思? –

+0

哪裏*確切*你想要這個下拉列表? –

+0

在homecontroller.cs中顯示的索引視圖頁面和歡迎查看頁面之間,只需一個包含男性和女性的下拉列表,當客戶選擇他/她所顯示的性別時,會顯示在使用會話的歡迎查看頁面中 – LohithT

回答

0

可以使用視圖模型到視圖之間傳輸數據(S )和行動方法。視圖模型只是一個簡單的POCO類。

using System.Collections.Generic; 
using System.Web.Mvc; 
public class WelcomeVm 
{ 
    public string Name {set;get;} 
    public string Gender {set;get;} 
    public IEnumerable<SelectListItem> Genders {set;get;} 
} 

現在,在您的性別GET行動,創建這樣的一個對象,負載Genders屬性,並以你的歡迎發送到視圖

public ActionResult Gender() 
{ 
    var vm = new WelcomeVm(); 
    vm.Genders = new List<SelectListItem>{ 
     new SelectListItem { Value="Male", Text="Male"}, 
     new SelectListItem { Value="FeMale", Text="FeMale"} 
    }; 
    return View(vm); 
} 

在您的性別觀點現在

@model WelcomeVm 
@using(Html.BeginForm("Welcome","YourControllerNameHere")) 
{ 
    <label>Name </label> 
    @Html.TextBoxFor(s=>s.Name) 
    <label>Gender</label> 
    @Html.DropDownListFor(s=>sGender,Model.Genders) 
    <input type="submit" /> 
} 

操作方法中,可以使用與參數類型相同的視圖模型,以便模型綁定完美地工作,並將發佈的表單數據映射到此類的對象。

[HttpPost] 
public ActionResult Welcome(WelcomeVm model) 
{ 
    return View(model); 
} 

,並在Welcome.cshtml Razor視圖,只顯示它

@model WelcomeVm 
<p>Hello @Model.Name who is a @Model.Gender</p> 
+0

它給我一個錯誤在我的Gender.cs頁面 – LohithT

+0

public IEnumerable Genders {set;得到; } – LohithT

+0

它表示無法找到名稱空間名稱'SelectListItem'的類型(您是否缺少using指令或程序集引用?) – LohithT