私はMVCにまだ慣れていませんが、本当に興味があります。私はビューの作成機能を持っていますが、自分がインデックスに作成した新しいデータを表示できないのはなぜですか?ここ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; }
}
誰もが私に言うことができません?おかげ
ここで表示/インデックスである:あなたのIndex
メソッド内
@{
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>
}
'Index()'とは何ですか?そしてなぜあなたは 'catch'ブロックに' return View( "Index");を持っていますか?そして、MVCのサイトに行き、チュートリアルを通して基本を学んでください。 POSTメソッドは 'FormCollection formCollection'ではなく' UserActivityModels model'でなければなりません( 'FormCollection'を使う必要はありません)。データを保存する場所はどこにもありません。 –
ここのインデックスは〜/ Views/Shared/Indexを意味します。したがって、create.cshtmlでは、新しいデータを作成してデータベースに格納し、「送信」ボタンをクリックすると新しいデータをインデックスに表示したいと考えています。なぜ新しいデータがdbに格納されていないのだろうか?私は既に@StephenMueckeの質問にcreate.cshtmlを提供しています。 – Botski
どこにでも、あなたは 'Create' POSTメソッドを使ってどこにでもデータを保存しますか?別の方法をリダイレクトしたとき(たとえその方法を表示していなくても)、どのように表示されると思いますか? MVCサイトに行き、基礎を学ぶ –