表单帖子传递null模型 - .NET MVC 4

时间:2013-04-02 11:36:19

标签: asp.net-mvc-4

我正在使用this post as reference

我正在尝试获取传递给视图的模型,以便在单击输入时回发到控制器的HttpPost方法。 但是,该模型(在本例中仅为List)在回发时为空。

我已将我的代码包含在内以供参考。这只是一个测试随机内容的项目,所以我为糟糕的代码道歉。

我有以下查看代码:(显示完整代码的完整代码)

@{
    ViewBag.Title = "Home Page";
}
@using TestApp.MyObjects
@model List<Contact>
@Ajax.ActionLink("Show About", "About", new { id = "1" }, new AjaxOptions { InsertionMode = InsertionMode.Replace, UpdateTargetId = "contentDiv" })
@Ajax.ActionLink("Show Contact", "Contact", new AjaxOptions { InsertionMode = InsertionMode.Replace, UpdateTargetId = "contentDiv" })
<div id="contentDiv"></div>
@using (Html.BeginForm())
{

    <table>
        @foreach (Contact c in Model)
        {
            <tr>
                <td>
                    <button aboutnum0 = "@c.someValues[0]" aboutnum1 = "@c.someValues[1]" aboutnum2 = "@c.someValues[2]" class="nameButton">@c.name</button>
                </td>
            </tr>
        }
    </table>
    <input value="@Model[0].name" />
    <input value="@Model[0].name" />
    <div id ="aboutContentDiv"></div>
    <input type="submit" />
    @ViewBag.myCoolValue
}
<script type="text/javascript">
    $("button").click(function () {
        $("#aboutContentDiv").empty();
        $("#aboutContentDiv").append($("<div></div>").load("Home/About/" + $(this).attr("aboutnum0")));
        $("#aboutContentDiv").append($("<div></div>").load("Home/About/" + $(this).attr("aboutnum1")));
        $("#aboutContentDiv").append($("<div></div>").load("Home/About/" + $(this).attr("aboutnum2")));
    });

</script>

以下是我的Comtroller代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TestApp.MyObjects;

namespace TestApp.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
            Contact c = new Contact();
            c.name = "Some Name";
            c.someValues = new List<string>();
            c.someValues.Add("1");
            c.someValues.Add("2");
            c.someValues.Add("3");
            Contact c1 = new Contact();
            c1.name = "Some Name1";
            c1.someValues = new List<string>();
            c1.someValues.Add("4");
            c1.someValues.Add("5");
            c1.someValues.Add("6");
            Contact c2 = new Contact();
            c2.name = "Some Name2";
            c2.someValues = new List<string>();
            c2.someValues.Add("7");
            c2.someValues.Add("8");
            c2.someValues.Add("9");
            List<Contact> clist = new List<Contact>();
            clist.Add(c);
            clist.Add(c1);
            clist.Add(c2);
            Session["myCoolValue"] = "Cool1";
            TempData["myCoolValue"] = "Cool2";
            return View(clist);
        }
        [HttpPost]
        public ActionResult Index(List<Contact> contacts)
        {
            string name = contacts[0].name;
            return View("Index",contacts);
        }
        public PartialViewResult About(string id = "")
        {
            ViewBag.Message = "Your app description page.";
            About a = new About();
            a.someValue = id + " _ modified by contoller";
            ViewBag.myCoolValue = "Cool";
            return PartialView("About",a);
        }

        public PartialViewResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return PartialView("Contact");
        }
    }
}

1 个答案:

答案 0 :(得分:1)

根据您对我的评论的回复,您需要这样的内容:

  // you can use foreach and have a counter variable or this
  for (int i = 0; i < Model.Count; i++)
  {
    // you do not want to use a partial view so let's do it this way
    // put this in an appropriate place in your code
    // like inside a tr or div, it's up to you
    @Html.TextboxFor(m => m[i].name)

  }