我还是MVC的新手,但对此非常好奇。我有我的创建视图功能,但我想知道为什么我无法显示我创建的新数据?
这是我的控制器:
//GET /UserActivity/Create
public ActionResult Create()
{
UserActivityModels ua = new UserActivityModels();
return View(userActivity);
}
//
// POST: /UserActivity/Create
[HttpPost]
public ActionResult Create(FormCollection formCollection)
{
try
{
UserActivityModels ua = new UserActivityModels();
ua.Id = Int32.Parse(formCollection["Id"]);
ua.CreatedBy = Int32.Parse(formCollection["CreatedBy"]);
ua.CreatedOn = DateTime.Parse(formCollection["CreatedOn"]);
ua.ModifiedBy = Int32.Parse(formCollection["ModifiedBy"]);
ua.ModifiedOn = DateTime.Parse(formCollection["ModifiedOn"]);
ua.ContactId = formCollection["ContactId"];
ua.StatusCode = Int32.Parse(formCollection["StatusCode"]);
ua.StateCode = Int32.Parse(formCollection["StateCode"]);
ua.EntityName = formCollection["EntityName"];
ua.EntityId = Int32.Parse(formCollection["EntityId"]);
ua.ActivityType = Int32.Parse(formCollection["ActivityType"]);
ua.ActivityStatus = formCollection["ActivityStatus"];
ua.DueDate = DateTime.Parse(formCollection["DueDate"]);
ua.ActualEndDate = DateTime.Parse(formCollection["ActualEndDate"]);
ua.MasqueradeOn = DateTime.Parse(formCollection["MasqueradeOn"]);
ua.MasqueradeBy = Int32.Parse(formCollection["MasqueradeBy"]);
return RedirectToAction("Index");
}
catch
{
return View("Index");
}
}
模特:
public class UserActivityModels
{
public int Id { get; set; }
public DateTime CreatedOn { get; set; }
public int CreatedBy { get; set; }
public int ModifiedBy { get; set; }
public DateTime ModifiedOn { get; set; }
public string ContactId { get; set; }
public string EntityName { get; set; }
public int EntityId { get; set; }
public int StatusCode { get; set; }
public int StateCode { get; set; }
public int ActivityType { get; set; }
public string ActivityStatus { get; set; }
public DateTime DueDate { get; set; }
public DateTime ActualEndDate { get; set; }
public DateTime MasqueradeOn { get; set; }
public int MasqueradeBy { get; set; }
public string ContactName { get; set; }
//public int TotalCount { get; set; }
// public List<string> userActivity { get; set; }
}
有人能告诉我为什么当我点击“提交”按钮时,新创建的数据现在会显示在索引中吗?感谢
以下是查看/索引:
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_LayoutMain.cshtml";
}
<h2>Index</h2>
@*<p>
@Html.ActionLink("Create New", "Create")
</p>*@
<h3>User Activity</h3>
@using (Html.BeginForm("index", null, FormMethod.Get))
{
<div class="row">
<div class="col-sm-8">
<div class="input-group">
<input type="text"
name="filter"
value="@ViewBag.filter"
class="form-control"
style="display: inline"
placeholder="Search by Contact Name or Entity Name" />
<span class="input-group-btn">
<button class="btn btn-default" type="submit">Go</button>
</span>
</div>
</div>
<div class="pull-right col-lg-1">
<a class="btn btn-success" data-modal="" href="/UserActivity/Create" id="btnCreate">
<span class="glyphicon glyphicon-plus"></span>
</a>
</div>
</div>
<div class="table-responsive" style="margin-top:5px;">
@{
var grid = new WebGrid(
Model,
canPage: true,
rowsPerPage: 5,
canSort: true);
//ajaxUpdateContainerId: "grid");
//grid.Bind(Model, rowCount: (int)ViewData["totalCount"], autoSortAndPage: false);
grid.Pager(WebGridPagerModes.All);
@grid.GetHtml(
htmlAttributes: new { id = "grid" },
// id for ajaxUpdateContainerId parameter
fillEmptyRows: false,
tableStyle: "table table-striped",
mode: WebGridPagerModes.All,
columns: grid.Columns(
//grid.Column("Id", "Id"),
grid.Column("Id", "Id", style: "col-lg-1", canSort: true),
grid.Column("CreatedBy", "CreatedBy", style: "col-lg-6"),
grid.Column("CreatedOn", header: "CreatedOn", style: "col-lg-2", canSort: true),
grid.Column("ModifiedBy", header: "ModifiedBy", style: "col-lg-2"),
grid.Column("ModifiedOn", header: "ModifiedOn", style: "col-lg-2"),
grid.Column("ContactId", header: "ContactId", style: "col-lg-2"),
grid.Column("EntityName", header: "EntityName", style: "col-lg-2"),
//grid.Column("EntityName", "EntityName", style: "col-lg-1", canSort: true),
grid.Column("EntityId", header: "EntityId", style: "col-lg-2"),
grid.Column("StatusCode", header: "StatusCode", style: "col-lg-2"),
grid.Column("StateCode", header: "StateCode", style: "col-lg-2"),
grid.Column("ActivityType", header: "ActivityType", style: "col-lg-2"),
grid.Column("ActivityStatus", header: "ActivityStatus", style: "col-lg-2"),
grid.Column("DueDate", header: "DueDate", style: "col-lg-2"),
grid.Column("ActualEndDate", header: "ActualEndDate", style: "col-lg-2"),
grid.Column("MasqueradeOn", header: "MasqueradeOn", style: "col-lg-2"),
grid.Column("MasqueradeBy", header: "MasqueradeBy", style: "col-lg-2"),
grid.Column("ContactName", header: "ContactName", style: "col-lg-2"),
//grid.Column("ContactName", "ContactName", style: "col-lg-1", canSort: true),
grid.Column(header: "Action", canSort: false, style: "action",
format: @<text>
@Html.Raw("<a data-modal='' href='/UserActivity/Details/" + item.Id + "' id='" + item.Id + "' title='Detail'> <span class='glyphicon glyphicon-search'> </span> </a>")
@Html.Raw("<a data-modal='' href='/UserActivity/Edit/" + item.Id + "' id='" + item.Id + "' title='Edit'> <span class='glyphicon glyphicon-edit'> </span> </a>")
@Html.Raw("<a data-modal='' href='/UserActivity/Delete/" + item.Id + "' id='" + item.Id + "' title='Delete'> <span class='glyphicon glyphicon-trash'> </span> </a>")
</text>)
));
}
</div>
}
答案 0 :(得分:0)
在Index
方法中,您必须检索在Create
方法中创建的模型,并在返回视图时将其用作参数。
将模型从一个控制器方法传递到另一个控制器方法的一种方法是使用TempData
字典。
所以你可以这样做:在Create
方法中,在致电return RedirectToAction("Index");
之前,将模型存储在TempData
中:
TempData["MyModel"] = ua;
然后,在Index
方法中,从TempData
检索模型并将其传递到视图中:
var myModel = (UserActivityModels)TempData["MyModel"];
return View(myModel);