2013-02-25 29 views
1

我已經在那裏我已經建立了各種各樣的形式的頁面。 我有2個下拉列表(profilelist & salarylist)。如何如果選擇/一個ListItem,如果項目在下拉列表中選擇要檢查並啓用/禁用文本框沒有選擇

我有這種形式3個文本框。我想要做的事情:

我有2個箱子,我在這裏創建一個新的配置文件和一個新的工資組,並將新的配置文件添加到profillist amd,並將該工資組添加到工資清單中。

現在我想直到salarylis和profillist兩個項目被選中禁用第三個框。一旦選擇了項目,應啓用文本框。

我的觀點:

@model KUMA.Models.EmployeeCardAdminModel 

@{ 
    ViewBag.Title = "Index"; 
    Layout = "~/Views/Shared/_Layout.cshtml"; 
} 

<div class="row bgwhite"> 
    <div class="twelve columns"> 
     <h2>Administration KUMA</h2> 
     <div class="row bgwhite">@using (Html.BeginForm("Index", "Admin"))     
      { 
       <div class="three columns"> 
        @Html.DropDownList("UserId", (SelectList)ViewBag.UserId, "--Välj anställd--") 
       </div> 
       <div class="three columns" style="margin-right:457px !important;"> 
        @Html.DropDownList("Salaryid", (SelectList)ViewBag.Salaryid, "--Välj LöneGrupp--") 
        <input style="float:left;" type="submit" value="Knyt" /> 
       </div> 
      } 

      @using (Html.BeginForm("Kompetens", "KumaAdmin")) 
      { 
       <div class="three columns" style="margin-right: 627px;"> 
        <h6>Kompetenser</h6> 
        <div style="width:456px;"> @Html.ListBox("kompetensId", (SelectList)ViewBag.KomId, new { placeholder = "Kompetenser" })</div><br/> 
        @Html.TextBoxFor(mm => mm.Kompetens, new { placeholder = "Ange Kompetens" }) 
        @*@Html.TextBoxFor(mm => mm.KompetensTest, new { placeholder = "Kompetens" }) 
        @Html.TextAreaFor(mm => mm.KompetensTest, new { placeholder = "Kompetens", rows="5", cols="80" })*@ 
        <input type="submit" style="margin-right: 205px;" value="Skapa"/><br/><br/> 
       </div> 
      } 
       @using (Html.BeginForm("Index", "KumaAdmin")) 
       { 
        <div class="three columns" style="margin-right: 627px;"> 
         @* <input name="profileTxtBox"type="text" style="width:97px; height:28px;" value="" />*@ 
         @Html.TextBoxFor(mm => mm.Profile, new { placeholder = "Ange Profil" })      
         @Html.TextBoxFor(mm => mm.SalaryGroup, new { placeholder = "Ange LöneGrupp" }) 
         <input type="submit" value="Skapa"/> 
        </div> 
       } 
      @* <div class="one columns" style=" margin-left: 100px;margin-right: 164px; margin-top: -33px;"> 
       <input name="profileTxtBox"type="submit" style="width:100px;" value="Add" /> 
      </div>*@ 
      <div class="five columns"></div> 
     </div> 
    </div 

>

控制器:

public class AAdminController : Controller 
    { 
     static List<Employee> list = new List<Employee>(); 
     //EmployeeCardAdminModel employee = new EmployeeCardAdminModel(); 
     // 
     // GET: /Admin/ 
     //[Authorize(Roles = "Admin")] 

     [HttpGet] 
     public ActionResult Index() 
     { 
      ViewBag.UserId = new SelectList(list, "Id", "Profile"); 
      ViewBag.Salaryid = new SelectList(list, "Id", "SalaryGroup"); 
      ViewBag.KomId = new SelectList(list, "Id", "Kompetens"); 


      ModelState.Clear(); 

      return View("Index"); 
     } 

     [HttpPost] 
     // submit for profile & salary box 
     public ActionResult Index(Models.EmployeeCardAdminModel e) 
     { 
      if (string.IsNullOrEmpty(e.Profile) == false && string.IsNullOrEmpty(e.SalaryGroup) == false) 
      { 
       // adda a new employye to the list and set the values from the parameter to the model 
       list.Add(new Employee 
        { 
         Id = e.Id + 1, 
         Profile = e.Profile, 
         SalaryGroup = e.SalaryGroup 
        }); 
      } 
      return (Index()); 
     } 

     [HttpPost] 
     // submit for knowledge box 
     public ActionResult Kompetens(Models.EmployeeCardAdminModel e) 
     { 
      if (string.IsNullOrEmpty(e.Kompetens) == false) 
      { 
       // adda a new employye to the list and set the values from the parameter to the model 
       list.Add(new Employee 
       { 
        Kompetens = e.Kompetens 
       }); 
      } 
      return (Index()); 
     } 

最後我的模型(注意emplyee類是一樣的我的模型,具有相同屬性但我認爲最好的做法是最好將這些分開。):

public class EmployeeCardAdminModel 
    { 

     public string Profile { get; set; } 
     public int Id { get; set; } 
     public string SalaryGroup { get; set; } 
     public string Kompetens { get; set; } 

    } 

的方式,我會normaly做,這是調用所需的列表,並檢查是否選擇指數大於零較大,但問題是我不知道如何從控制器訪問以正確的方式列表,所以我可以訪問它中的項目。我怎麼能得到一個文本框上的ID?我需要這個能夠禁用/啓用正確的文本框。

林很新的MVC和我做項目,因此所有的建議表示讚賞學習。

謝謝!

回答

1

那麼一段時間後,我意識到,有沒有這樣做沒有腳本的一個好辦法。

所以我用jQuery解決了這個問題是這樣的:

$(function() { 

    $(".list select").change(function() { 
     if ($(".list1 select").val().length == 0 || $(".list2 select").val().length == 0) { 

      $(".kompetensbox input").attr('disabled', 'disabled'); 
     } 

     else { 
      $(".kompetensbox input").removeAttr('disabled'); 
     } 
    }) 
}); 

視圖改變(添加CSS類):

 <div class="three columns list list1"> 
      @Html.DropDownList("UserId", (SelectList)ViewBag.UserId, "--Välj profilgrupp--") 
     </div> 
     <div class="three columns list list2" style="margin-right:457px !important;"> 
      @Html.DropDownList("Salaryid", (SelectList)ViewBag.Salaryid, "--Välj LöneGrupp--") 
      <input style="float:left;" type="submit" value="Knyt" /> 
     </div> 

希望它可以幫助其他人試圖相同。

相關問題