2009-07-22 3 views
0

私はUserControllerとEdit.aspxを持っています。私の主キーなので、私はこのフィールドを編集するユーザーを許可したくない1つのフィールドがあります。asp.net-mvc/linq to sql - 編集保存を行うには、HTML.TextBoxが常に必要ですか?

[AcceptVerbs(HttpVerbs.Post)] 
    public ActionResult Edit(int id, tblMailingList user_) 
    { 
     try 
     { 
      repo.UpdateUser(user_); 
      return RedirectToAction("Index"); 
     } 
     catch 
     { 
      return View(); 

tblMailingListの電子メールフィールドがnull:

問題は、私は

 <%= Html.TextBox("Email", Model.Email) %> 

を削除した場合、その後asp.net-MVC魔法は私のコントローラのコードを呼び出したときにということです。問題は、現在のレコードを取得するためにテーブルのルックアップとしてこれを必要とし、明らかにそのnullが例外を取得するかどうかです。

このフィールドにテキストボックスを戻すと、正常に動作します。私はテキストボックスを持っていて、このフィールドをコントローラに渡すための編集ができるようにしなければならないと夢中に思えます。私はラベルに入れてみましたが、まだコントローラにnullとして表示されます。

提案がありますか?

答えて

3

私の最初の質問は、IdフィールドではなくEmailフィールドで検索している理由でしょうか?

フォーム宣言のパラメータを渡して、コントローラに渡すことができます。

<% using (Html.BeginForm(
    "MethodName", "Controller", FormMethod.Post, 
    new { id = Model.Id, email = Model.Email)) { %> 

メソッドの宣言が正しいかどうかわかりませんので、確認してください。

[AcceptVerbs(HttpVerbs.Post)] 
public ActionResult Edit(int id, string email, tblMailingList user_) 
{ 
    try 
    { 
     repo.UpdateUser(user_); 
     return RedirectToAction("Index"); 
    } 
    catch 
    { 
     return View(); 

tblMailingListのユーザーがリポジトリで更新されないため、少し異なる方法で更新することをお勧めします。

[AcceptVerbs(HttpVerbs.Post)] 
public ActionResult Edit(int id, FormCollection form) 
{ 
    tblMailingList user = repo.GetUser(id); // get the user using the id 
              // so we can update in the same 
              // context 
    UpdateModel(user); // this will automatically update 
         // your user model with values 
         // from the form 

    try 
    { 
     repo.UpdateUser(user); 
     return RedirectToAction("Index"); 
    } 
    catch 
    { 
     return View(); 
0

フォームに表示されないようにする必要があるコントローラに渡すことのできるフィールドが必要な場合は、Html.HiddenFieldを使用することができます。

間違っていますか?

関連する問題