0

我用这篇文章作为参考

我试图让我传递给视图的模型在单击输入时发回控制器的 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>

以下是我的控制器代码:

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");
        }
    }
}
4

1 回答 1

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)

  }
于 2013-04-02T12:24:38.223 回答