2016-05-05 15 views
0

私は、フォームデータを持つ標準的なカミソリビューで作業しています。送信ボタンを押した後、入力フォームのデータをコントローラに渡したいと思います。私の考えは次のようになります:ビューからコントローラへのフォーム入力の受け渡し

@model CareSource.ReleaseAssistant.Models.Prime.Team 

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

<h2>Create New Team</h2>  
    @using (Html.BeginForm()) 
    { 
     @Html.AntiForgeryToken() 

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

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

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

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

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

      <div class="form-group"> 
       @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" }) 
       <div class="col-md-10"> 
        @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } }) 
        @Html.ValidationMessageFor(model => model.Name, "", 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> 

    <script src="~/Scripts/jquery-1.10.2.min.js"></script> 
    <script src="~/Scripts/jquery.validate.min.js"></script> 
    <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script> 

フォームの情報が私のアクションメソッドによって処理されるために私のコントローラに戻されていないという問題があります。私はWeb APIを使用してデータベースに新しいエントリを作成しようとしています。私のアクションメソッドは、次のようになります。

Codesmithジェネレータを使用して生成されたこのプロジェクトのコードのほとんど
[HttpPost] 
public ActionResult Create(FormCollection collection) 
{ 
    try 
    { 
     if (ModelState.IsValid) 
     { 
       HttpEndPointContext httpEndPoint = new HttpEndPointContext() 
       { 
        AuthenticationMethod = HttpAuthenticationMethods.None, 
         Ssl = false, 
         HttpMethod = HttpMethod.Post, 
         //Path = "localhost:32173/api/team/", 
         QueryStrings = null, 
       }; 

       IProcessResult result = HttpConnectionManager.Current.SendMessage(httpEndPoint); 

       var response = result.ResponseData.ToString(); 
      } 
       return RedirectToAction("Index"); 
    } 
    catch 
    { 
      return View(); 
    } 
} 

とはPetaPocoと呼ばれるMicroORMを使用しています。

@using (Html.BeginForm("Create", "YourControllerName", FormMethod.Post,  new { id = "form" })) 
{ 
    //Copy your current code as it is here.  
} 
+0

私の答えに記載されているとおりにビューを更新するだけです。助けを知らせてください – Dilip

+0

私の答えを確認した後に教えてください – Dilip

+0

FormCollectionの代わりにチームモデルを渡してください – Dilip

答えて

0

与えられたコードを使用してビューを更新してください入力フォームフィールドを生成するには、HttpPostメソッドのパラメータとしてTeamオブジェクトを使用できます。

HttpPost] 
public ActionResult Create(Team model) 
{ 
    if (ModelState.IsValid) 
    { 
     //access model.Properties 
    } 
    return View(model); 
} 
+0

私はちょっと前にこれを試しました。フォームフィールドをコントローラメソッドに渡していますが、フィールド内のデータは渡していません。私はここに何かを逃していますかこのようにコードを変更し –

+0

: 公共のActionResultは(チームモデル)を作成します { 場合(ModelState.IsValid) {// アクセスmodel.Properties } リターンビュー(モデル); } – Dilip

0

があなたのフォームが強くTeamのインスタンスにタイプされているので、パラメータとして使用するチームモデルはformCollectionを削除し、

1

を試していますし、あなたはそれを使用している:

関連する問題