2016-10-22 2 views
-1

モデルは私が既存の1に作成するので、おそらく私のすべての方法が間違っています。MVCはどのようにドロップダウンリストから値を提出する私はそれをよりよく説明するが、あなたが行う前に、私の目標はとの関係を作ることです、他のモデル <p>私の手順に従ってください</p>のリストからIDを取得したモデルを作成しようとしています

これは2つのモデルの関係
ClientIdを<ある - > ClientIdを
enter image description here

私の手順は以下のとおりであった:

ステップ1:私は、このビューモデル

public class SubscriptionViewModel 
{ 
    [Display(Name = "Client")] 
    public int clientId { get; set; } 
    [Required] 
    [Display(Name = "Domain")] 
    public string clientDomain { get; set; } 
    [Required] 
    [Display(Name = "Time")] 
    public int subscriptionTime { get; set; } 
    [Display(Name = "Document Access Key")] 
    public string documentAccKey { get; set; } 

} 

手順を作成しました2:私のコントローラで私はクライアントリストを取得し、ビューデータに入れた

マイ[GET]アクションを作成

public ActionResult Create() 
    { 
     ViewData["clientList"] = DbHandlers.GetClientsList(); 
     return View(); 
    } 

enter image description here

ステップ3:ViewDataを

マイビューから作成されたドロップダウンリスト:

@model AccessibilityServiceControlPabel.Models.SubscriptionViewModel 
 

 
@{ 
 
    ViewBag.Title = "Create"; 
 
} 
 

 
<h2>Create</h2> 
 

 
@using (Html.BeginForm()) 
 
{ 
 
    @Html.AntiForgeryToken() 
 
    
 
    <div class="form-horizontal"> 
 
     <h4>SubscriptionViewModel</h4> 
 
     <hr /> 
 
     @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
 

 
     <div class="form-group"> 
 
      @Html.LabelFor(model => model.clientId, htmlAttributes: new { @class = "control-label col-md-2" }) 
 
      <div class="col-md-10"> 
 
       @Html.DropDownList("SelectedEmployee",new SelectList((System.Collections.IEnumerable)ViewData["clientList"], "ClientId", "ClientName"), "--Select--", new { @class = "form-control" }) 
 
      </div> 
 
     </div> 
 

 
     <div class="form-group"> 
 
      @Html.LabelFor(model => model.clientDomain, htmlAttributes: new { @class = "control-label col-md-2" }) 
 
      <div class="col-md-10"> 
 
       @Html.EditorFor(model => model.clientDomain, new { htmlAttributes = new { @class = "form-control" } }) 
 
       @Html.ValidationMessageFor(model => model.clientDomain, "", new { @class = "text-danger" }) 
 
      </div> 
 
     </div> 
 

 
     <div class="form-group"> 
 
      @Html.LabelFor(model => model.subscriptionTime, htmlAttributes: new { @class = "control-label col-md-2" }) 
 
      <div class="col-md-10"> 
 
       @Html.EditorFor(model => model.subscriptionTime, new { htmlAttributes = new { @class = "form-control" } }) 
 
       @Html.ValidationMessageFor(model => model.subscriptionTime, "", new { @class = "text-danger" }) 
 
      </div> 
 
     </div> 
 

 
     <div class="form-group"> 
 
      @Html.LabelFor(model => model.documentAccKey, htmlAttributes: new { @class = "control-label col-md-2" }) 
 
      <div class="col-md-10"> 
 
       @Html.EditorFor(model => model.documentAccKey, new { htmlAttributes = new { @class = "form-control" } }) 
 
       @Html.ValidationMessageFor(model => model.documentAccKey, "", new { @class = "text-danger" }) 
 
      </div> 
 
     </div> 
 

 
     <div class="form-group"> 
 
      <div class="col-md-offset-2 col-md-10"> 
 
       <input type="submit" value="Create" class="btn btn-default" /> 
 
      </div> 
 
     </div> 
 
    </div> 
 
} 
 

 
<div> 
 
    @Html.ActionLink("Back to List", "Index") 
 
</div>

そして、それは私の問題のために今

enter image description here

私の見解で正常に見える:ビューを提出した後
、私が選択した値のclientIdを得ることはありません。

マイ[HttpPost]アクションを(まだ完了していない)

public ActionResult Create(SubscriptionViewModel subscriptionviewmodel) 
    { 
     var subscription = new Subscriptions(); 

     return RedirectToAction("Index"); 
    } 

subscriptionviewmodelデータはのclientIdデータ

が含まれていない、これは私が私の見解では、挿入したデータで作成

enter image description here

投稿後、これが私の得意です。
enter image description here

ご覧のとおり、cientIdは0で、私の選択に従って1になっています。
誰かが私が間違ったところにいるのを助けてくれますか?

クライアントIDを取得するために私の方法が間違っている可能性があります。本当に必要なのは、すべてのクライアントモデルを送信することです。
私の主な目的は、それらの間の関係を作ることだと思います。あなたのケースではmodel名に事前に

おかげ

答えて

1

変更し、あなたのドロップダウンのNameは、SelectedEmployeeへのclientIdから名前を変更します。

<div class="form-group"> 
      @Html.LabelFor(model => model.clientId, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.DropDownList("clientId",new SelectList((System.Collections.IEnumerable)ViewData["clientList"], "ClientId", "ClientName"), "--Select--", new { @class = "form-control" }) 
      </div> 
     </div> 
+0

作品、ありがとうKumar – Bglen

+0

嬉しいです! –

1

DropDownList(fistparameter、...)で返されるフィールドを渡すことはありません。なぜフォームポスト中にフォームに戻って値を返さないのですか? DropDownList( "SelectEmployee"、..)の最初のパラメータをモデルプロパティ名に変更します。

@Html.DropDownList("clientId",new SelectList((System.Collections.IEnumerable)ViewData["clientList"]....) 
+0

作業中、ありがとうRajput – Bglen

関連する問題

 関連する問題