我使用this post as reference表格後傳遞空模型 - .NET MVC 4
我試圖讓我傳遞給視圖點擊輸入時回發到控制器的HttpPost方法的模型。 但是,該模型(在本例中僅爲List)在回發時爲空。
我已經包含了我的代碼以供參考。這只是一個測試隨機東西的項目,所以我爲蹩腳的代碼道歉。
我有以下視圖代碼:(顯示爲completness整個代碼)
@{
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");
}
}
}
您是打算接受模型的輸入,或者你只是想發回它?如果您打算接受輸入,您是否有聯繫模式的(部分)視圖? –
我經常會看到Contact模型的部分視圖。我猜在這個測試中,我不知道。我認爲這不是必須的,我對聯繫人有部分看法?例如,您可以看到我將第一個聯繫人的姓名放入輸入中。這應該允許進行編輯,並允許我在回發後查看編輯。或者我不明白這是如何工作的? – kralco626
您創建嵌套'@ Model.name'的方式看起來不錯。你的意思是創建兩次輸入還是那個粘貼錯誤? –