1

以下のコードは、コントローラーに null 値を渡して送信した後に入力したサンプルです。コントローラーでは、クラス名を使用して値を正しく渡しましたが、パラメーターを使用すると、NULL 値がコントローラーに渡されます。解決策を教えてください..

コントローラ:

[HttpGet]
        public ActionResult Index()
        {
            return View();
        }


        [HttpPost]
        public ActionResult Index(string firstname)
        {
            LogonViewModel lv = new LogonViewModel();
            var ob = s.Newcustomer(firstname)

            return View(ob );
        }

意見:

@model IList<clientval.Models.LogonViewModel>

@{
    ViewBag.Title = "Index";
}

@using (Html.BeginForm())
{
    for (int i = 0; i < 1; i++)
    {  
    @Html.LabelFor(m => m[i].UserName)
    @Html.TextBoxFor(m => m[i].UserName)
     @Html.ValidationMessageFor(per => per[i].UserName)

    <input type="submit" value="submit" />
    }
}

モデル:

 public class LogonViewModel
    {
        [Required(ErrorMessage = "User Name is Required")]
        public string UserName { get; set; }
    }



    public List<ShoppingClass> Newcustomer(string firstname1)
        {

            List<ShoppingClass> list = new List<ShoppingClass>();
           ..
        }
4

2 に答える 2

0

これ:

[HttpGet]
public ActionResult Index() {
    return View();
}

あなたの見解ではこれを与えません:

@model IList<clientval.Models.LogonViewModel>

この:

for (int i = 0; i < 1; i++) {  
    @Html.LabelFor(m => m[i].UserName)
    @Html.TextBoxFor(m => m[i].UserName)
     @Html.ValidationMessageFor(per => per[i].UserName)

    <input type="submit" value="submit" />
}

これでは動作しません:

[HttpPost]
public ActionResult Index(string firstname) {
      LogonViewModel lv = new LogonViewModel();
      var ob = s.Newcustomer(firstname)
      return View(ob );
}

モデルをビューに送信しておらず、ビューでリストを使用していますが、コントローラーで単一の文字列値を期待しています。あなたの例、またはコードに何かが非常に奇妙/間違っています。

モデルとして IList を使用するのはなぜですか? 単一の入力フィールドを持つフォームをレンダリングするだけでよい場合。次のようなコードが必要です。

[HttpGet]
public ActionResult Index() {
    return View(new LogonViewModel());
}

そしてビュー:

@model clientval.Models.LogonViewModel
@using (Html.BeginForm())
{
    @Html.LabelFor(m => m.UserName)
    @Html.TextBoxFor(m => m.UserName)
    @Html.ValidationMessageFor(m => m.UserName)

    <input type="submit" value="submit" />
}

コントローラーの 2 番目のアクション:

[HttpPost]
public ActionResult Index(LogonViewModel model) {
    if (ModelState.IsValid) {
      // TODO: Whatever logic is needed here!
    }
    return View(model);
}
于 2012-07-30T05:47:49.343 に答える
0

その作業..私は以下に書かれているように私のコントローラを変更しました

コントローラ:

[HttpGet]
        public ActionResult Index()
        {
            return View();
        }


        [HttpPost]
        public ActionResult Index(IList<LogonViewModel> obj)
        {

            LogonViewModel lv = new LogonViewModel();
            var ob = lv.Newcustomer(obj[0].FirstName)

            return View(ob );
        }
于 2012-07-30T11:44:54.563 に答える