0

テキストボックス (名前と呼ばれる) とチェックボックスのセットとラジオのセットを持つフォームがあります。テキストボックスの値、チェックされているチェックボックスの値とチェックされているラジオの値のみを含む配列を送信したい。これには jquery ajax 構文を使用できることはわかっていますが、Ajax.BeginForm で実行したいと考えています。私が言えることから、AjaxオプションのOnBeginがこれの鍵かもしれませんが、アクションに進む前にリクエストを変更する方法がわかりません。

****編集:****

スティーブンの答えに基づいてコードを作り直しましたが、適切にレンダリングするだけでまだ問題が発生しています。コードは次のとおりです。

public class RecipeSearch
{
    public List<Recipe> Recipe { get; set; }
    public List<Meal> MealSettings { get; set; }
    public List<Ingredient> MainIngredient { get; set; }
}

public class Meal
{
    public int Id { get; set; }
    public bool Value { get; set; }
    public string DisplayName { get; set; }

}

    public ActionResult Search()
    {

        RecipeSearch recipe = new RecipeSearch();
        recipe = recipeSearchDetails(null, null);

        recipe.Recipe = dbContext.Recipes
            .OrderBy(r => r.name)
            .Where(r => r.name.Contains("") || string.IsNullOrEmpty(""))
            .ToList();

        RecipeSearch recipe = new RecipeSearch();
        recipe.MealSettings = new List<Meal>();
        var meals = dbContext.MealCategories
                    .OrderBy(r => r.name)
                    .ToList();

        var MainIngredient = dbContext.Maincategories
                    .OrderBy(r => r.name).ToList();

        foreach (var item in meals)
        {
            recipe.MealSettings.Add(new Meal { DisplayName = item.name, Value = true, Id = item.mealCategoryId });
        }



        recipe.Recipe = dbContext.Recipes
                        .OrderByDescending(r => r.MealCategory.name)
                        .Where(mc => mc.MealCategory.name == mealCategory || (mealCategory == null))
                        .Where(m => m.Ingredients.FirstOrDefault().SubCategory.name == mainIngredient || (mainIngredient == null)).ToList();

        return PartialView("_search", recipe);
    }

モデル:

@model RecipeTrackerMVC.Models.Search.RecipeSearch


@for (int i = 0; i < Model.MealSettings.Count(); i++)
   {
      <li>
           @Html.CheckBoxFor(r => Model.MealSettings[i].Value)
           @Html.LabelFor(r => Model.MealSettings[i].Value, Model.MealSettings[i].DisplayName)
                                @Html.HiddenFor(r => Model.MealSettings[i].Id)
      </li>
   }

ブラウザーは、食事ごとに以下をレンダリングしています。 ここに画像の説明を入力

名前はとてもずさんにレンダリングすることになっていますか?

それに加えて、AJAX 呼び出しを行い、これが取得したデータです。ID 12 を除いてすべての項目がチェックされていませんでしたが、何らかの理由で 1 つに 2 つの値があり、その理由がわかりません。また、このスタイル (checkboxfor) では、チェックされたアイテムのみがリクエストで送信されるはずだと思いましたか?

name:s
allCategories:false
MealSettings[0].Value:false
MealSettings[0].Id:7
MealSettings[1].Value:false
MealSettings[1].Id:8
MealSettings[2].Value:false
MealSettings[2].Id:9
MealSettings[3].Value:false
MealSettings[3].Id:1
MealSettings[4].Value:false
MealSettings[4].Id:10
MealSettings[5].Value:false
MealSettings[5].Id:11
MealSettings[6].Value:false
MealSettings[6].Id:12
MealSettings[7].Value:true
MealSettings[7].Value:false
MealSettings[7].Id:4
MealSettings[8].Value:false
MealSettings[8].Id:3
MealSettings[9].Value:false
MealSettings[9].Id:17
MealSettings[10].Value:false
MealSettings[10].Id:2
allIngredients:all
allIngredients:false
startTime:
endTime:
prepTime:0
cookTime:0
standTime:0
X-Requested-With:XMLHttpRequest

そして、この構文で Ajax 呼び出しを行うと、モデルは null として表示されます。

    public ActionResult Search(IEnumerable<RecipeSearch> model)
4

1 に答える 1

1

MealCategoryがブール値のリストであると仮定すると、使用@Html.CheckBoxForするとポストバック用の html が生成されます (ラジオ ボタンと同様)。

@model yourModel
@using (Html.BeginForm()) {
  ....
  @for (int i = 0; i < Model.MealCategory.Count; i++) {
    @Html.CheckBoxFor(m => m[i])
    @Html.LabelFor(m => m[i])
  }
  ...
  <button type="submit">Submit</button>
}

編集:コメントに加えて、あなたが何をしようとしているのか完全にはわかりませんが、可能な選択を表すビューモデルを作成し、コントローラーでアクションメソッドを作成して、次のようにオプションを選択することをお勧めします:

public class MealSelection
{
  public int ID { get; set;} // to generate a hidden input for postback
  public bool IsSelected { get; set;} // to generate a checkbox
  public string Name { get; set;} // for display in the view
}

[HttpGet]
public ActionResult Selections()
{  
  List<MealSelection> selections = new ..// Generate a list selections
  return View(selections)
}

[HtpPost]
public ActionResult Selections(IEnumerable<MealSelection> model)
{
  IEnumerable<MealSelection> selections = model.Where(m => m.IsSelected);
  // foreach MealSelection in selections, get the ID and do something with it
  return RedirectToAction(..to some view that displays the selections..);
}

意見

@model IEnumerable<MealSelection>

次に、ループ (上記を変更) の隠し入力を作成しID、 のチェックボックスIsSelectedと のラベルを作成しNameます。

于 2014-05-25T23:48:33.737 に答える