2016-09-02 22 views
0

ASP.NET MVCを初めて使用しています。テーブルのレコードのリストからレコードを取得しようとしています。各レコードには、statusというフィールドがあり、1に設定されています。ここでは、ViewButtonのレコードが1つだけ表示されています。ボタンをクリックすると、そのレコードのステータスをデータベースで3に変更し、同じビューで別のレコードを取得する必要があります。現在私はScaffolding template Listを選択してこのビューを作成しました。ASP.NET MVCのテーブルの特定の列を変更します。

コントローラー:

public class HomeController : Controller 
{ 
    dbSampleEntities db = new dbSampleEntities(); 
    // 
    // GET: /Home/ 
    public ActionResult Index() 
    { 
     var result = db.Visits.Where(x=> x.Status == "1").OrderBy(r => Guid.NewGuid()).Take(1); 
     return View(result); 
    } 

    [HttpPost] 
    public ActionResult ChangeStatus(int VisitorID) 
    {   
     return RedirectToAction("Index");   
    } 
} 

Index.htmlと:ここ

@model IEnumerable<SampleApp.Visit> 

@{ 
    ViewBag.Title = "Index"; 
} 

<table> 
    <tr> 
     <th> 
      @Html.DisplayNameFor(model => model.VisitorID) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.FirstName) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.LastName) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.Phone) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.VisitTypeID) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.BranchID) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.Status) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.EmailID) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.Comments) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.UserCreated) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.CreatedTime) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.UpdatedTime) 
     </th> 
     <th></th> 
    </tr> 

    @foreach (var item in Model) 
    { 
     <tr> 
      <td> 
       @Html.DisplayFor(modelItem => item.VisitorID) 
      </td> 
      <td> 
       @Html.DisplayFor(modelItem => item.FirstName) 
      </td> 
      <td> 
       @Html.DisplayFor(modelItem => item.LastName) 
      </td> 
      <td> 
       @Html.DisplayFor(modelItem => item.Phone) 
      </td> 
      <td> 
       @Html.DisplayFor(modelItem => item.VisitTypeID) 
      </td> 
      <td> 
       @Html.DisplayFor(modelItem => item.BranchID) 
      </td> 
      <td> 
       @Html.DisplayFor(modelItem => item.Status) 
      </td> 
      <td> 
       @Html.DisplayFor(modelItem => item.EmailID) 
      </td> 
      <td> 
       @Html.DisplayFor(modelItem => item.Comments) 
      </td> 
      <td> 
       @Html.DisplayFor(modelItem => item.UserCreated) 
      </td> 
      <td> 
       @Html.DisplayFor(modelItem => item.CreatedTime) 
      </td> 
      <td> 
       @Html.DisplayFor(modelItem => item.UpdatedTime) 
      </td> 
      <td> 
       @using (Html.BeginForm("ChangeStatus", "Home", FormMethod.Post)) 
       { 
        @Html.HiddenFor(modelItem => item.VisitorID) 
        <input type="submit" value="Change Status" /> 
       } 
      </td> 
     </tr> 
    } 

</table> 

ChangeStatusは、呼び出しが、NULL値を得ています。私が間違っているところで助けてください。

+1

'ChangeStatus'であなたの' Return View(); 'をRedirectToAction( 'Index')を返すように変更してみてください。 – techspider

+0

返事ありがとうございます。そんなこと知ってる。しかし、そのPOSTメソッドでは、ステータス列を1から3に更新する必要があります。しかし、** ChangeStatus(Visit visit)ですべての値がnullになっています** – NNR

答えて

1

フォームはChangeStatusに転記されていますが、フォームの値が転記されていません。フォームに実際のフォーム要素がないため、投稿するデータはありません。したがって、フレームワークにはVisitのインスタンスを構築する値はありません。

レコードに対して1つの事前定義済み操作を実行しているだけなので、実際に必要なのは、そのレコードの識別子です。レコード全体は必要ありません。その識別子を使用すると、データベースからレコードを取得し、更新して保存します。

だから、全体的なフォームを削除し、各テーブルの行の1を追加し、その中であなたは、識別子のためのフォーム要素含まれます:

<td> 
    @using (Html.BeginForm("ChangeStatus", "Home", FormMethod.Post)) 
    { 
     @Html.HiddenFor(modelItem => item.VisitorID) 
     <input type="submit" value="Change Status" /> 
    } 
</td> 

(注:私はVisitorIDが識別子である推測しているがレコードの命名はいないものの、かなりラインを100%使用どんな識別子、または識別子のセット、そのレコードのために存在する)アクションはちょうど識別子期待に続いて

アップ:。。。

+0

あなたが言ったように試してみてください。しかし、このエラーを取得しています**パラメータ辞書には、 'NiranjanApp.Controllers.HomeController'の 'System.Web.Mvc.ActionResult ChangeStatus(Int32)'メソッドのnullable型 'System.Int32'のパラメータ 'id'のnullエントリが含まれています'オプションのパラメータは、参照型、null可能型、またはオプションのパラメータとして宣言する必要があります。 パラメータ名:パラメータ** – NNR

+0

@NNR:試した内容を表示します。パラメータの名前とフォーム要素の名前は一致しなければなりません。フレームワークがそれらを使用する方法を知っています。 – David

+0

私の質問を変更しました – NNR

関連する問題