2017-10-18 26 views
0

問題私は1つの隠しフィールドを持つフォームを持っており、送信ボタンをカスタム列とのWebGridを使用していますWebGridの列が通らない非表示フィールド

。 HTMLソースを見ると、隠しフィールドに値があることがわかります。フォームはコントローラ内で正しいHttpPostメソッドを送信し、ヒットします。問題は、隠されたフィールドが投稿と一緒に行かないことです。ここ

コード

はWebGridのを生成するコードは、次のとおり

WebGrid wgSearchResults = new WebGrid(
    source: Model.notes, 
    rowsPerPage: 10, 
    canPage: true, 
    canSort: true 
); 

wgSearchResults.Pager(WebGridPagerModes.All, numericLinksCount: 10); 

@wgSearchResults.GetHtml(
    htmlAttributes: new { id = "wgSearchResults" }, 
    mode: WebGridPagerModes.All, 
    columns: wgSearchResults.Columns(
     wgSearchResults.Column(format: (item) => 
      { 
       System.Text.StringBuilder html = new System.Text.StringBuilder(); 

       html.Append("<form action='/ManageData/ViewFacilities' method='post'>"); 
       html.Append("<input type='hidden' id='hidBusinessId_' value='" + item.businessId.ToString() + "' />"); 
       html.Append("<input type='submit' id='btnViewFacilities_" + item.ID.ToString() + "' name='btnSubmit_" + item.ID.ToString() + "' value='View Facilities' />"); 
       html.Append("</form>"); 

       return new HtmlString(html.ToString()); 
      }, 
     canSort: false 
     ), 
     wgSearchResults.Column(columnName: "note", header: Sorter("note", "Note", wgSearchResults), canSort: true) 
    ) 
) 

Model.notes構造は、このクラスのインスタンスのリストである:

public class SearchNotesResult 
{ 
    public int ID { get; set;} 
    public int businessId { get; set; } 
    public string note { get; set; } 
} 

コントローラこのボタンがヒットするメソッド:

[HttpPost] 
public ActionResult ViewFacilities(string hidBusinessId) 
{ 
    int businessId = 0; 
    bool isValidNumber = false; 

    isValidNumber = int.TryParse(hidBusinessId, out businessId); 

    if (isValidNumber) 
    { 
     if (businessId > 0) 
     { 
      TempData["BusinessId"] = businessId; 

      return RedirectToAction("GoHere"); 
     } 
     else 
      return RedirectToAction("DontGoHere"); 
    } 
    else 
    { 
     return RedirectToAction("DontGoHere"); 
    } 
} 

上記の方法では、hidBusinessIdの値はnullです。

試み

  1. は、カミソリの構文(<text>タグ付き@)を使用してこのコラムを再構築
  2. (のみ送信ボタンを含まれている)上記のメソッドにパラメータFormCollection formを追加します。運がない。
  3. 数時間の研究。それが私がここにいる理由です。

ボタンIDにビジネスIDを追加し、

  • 任意の他に方法でコミュニティを解析
  • 取得するには、このポスト(およびHtml.ActionLinkなどの異なる制御)を変更するオプション

    1. 提案する

    ありがとうございました!

  • +0

    あなたは隠された入力に 'name'属性がないので、投稿するものは何もありません(無効な' id'属性を削除し、 'name = 'hidBusinessId''を使います)。また、送信ボタンから無意味な 'id'属性と' name'属性を削除することもできます。そして 'int'からメソッドのパラメータは' string'ではなく 'int hidBusinessId'でなければなりません –

    答えて

    0

    非常にばかげた間違い... idの代わりにname属性が必要でした。

    html.Append("<form action='/ManageData/ViewFacilities' method='post'>"); 
    html.Append("<input type='hidden' name='hidBusinessId' value='" + item.businessId.ToString() + "' />"); 
    html.Append("<input type='submit' value='View Facilities' />"); 
    html.Append("</form>"); 
    

    文字列データ型は機能しましたが(以下のコメントを参照してください)。迅速で正確な回答をいただきありがとうございます。

    +0

    重複する' id'属性は無効ですhtml !! (そしてあなたのボタンの 'name'属性は無意味です)そして' int'を 'string'パラメータにポストして手動で変換するのはちょっと狂っています。 –

    +0

    このコメントありがとうございます。私は追加の調整(不要なIDと名前の値を取り除いた)を行い、物事は意図どおりに機能します。投稿メソッドのhidBusinessIdパラメータのデータ型がstringからintに変更された場合は、隠しフィールドの値を数値以外の値に変更してフォームを送信すると、システムエラーが発生しやすくなります。システム全体のエラー処理モジュールがこれを処理することができます(後で説明します)。再度、感謝します! – AnakinL82

    関連する問題