我正在學習asp.net mvc,使用visual studio community 2017,並且作爲一種教學項目,我正在製作一個web應用程序,用於跟蹤鍛鍊情況。我的模型由WorkOut對象組成,這些對象具有練習的列表(或更具體的ICollection),並且每個練習都有一個ICollection。下面是我的模型類的基礎知識。如何構建asp.net mvc中複雜對象的創建視圖?
public class WorkOut
{
public int Id { get; set; }
public int Length { get; set; }
public DateTime Date { get; set; }
public virtual ICollection<Exercise> ExerciseList { get; set; }
}
public class Exercise
{
public int Id { get; set; }
public string Name { get; set; }
public int WorkOutId { get; set; }
public virtual WorkOut WorkOut { get; set; }
public virtual ICollection<RepUnit> Sets { get; set; }
}
public class RepUnit
{
public int Id { get; set; }
public int Rep { get; set; }
public int? Weight { get; set; }
public int ExerciseId { get; set; }
public virtual Exercise Exercise { get; set; }
}
使用WorkOut自動生成視圖作爲模型導致創建操作和視圖,只生成Length和Date屬性。通常,自動生成的視圖和控制器僅添加非虛擬屬性。所以我想,也許我必須做一個多步驟的創作過程;創建鍛鍊,創建鍛鍊並添加鍛鍊,將鍛鍊添加到鍛鍊中,停止鍛鍊或添加其他鍛鍊。所以我認爲Id讓VS對我來說是一些工作,並且我爲每個模型對象類型(WorkOutsController,ExercisesController,RepUnitsController)製作控制器和視圖,稍後我會修剪出不重要的視圖,甚至重構操作i實際上使用到一個新的控制器。 所以WorkOutController我的POST動作是這樣的。
public ActionResult Create([Bind(Include = "Id,Length,Date")] WorkOut workOut)
{
if (ModelState.IsValid)
{
db.WorkOuts.Add(workOut);
db.SaveChanges();
return RedirectToAction("Create","Exercises",new { workoutId = workOut.Id });
}
return View(workOut);
}
因此,我將workoutI運行到運動控制器,但這是我不確定如何繼續。我想繼續攜帶workoutId,並在下一步中爲練習命名,同時顯示剛剛添加的相關日期。我唯一能想到的就是像Exercise一樣在ExerciseController的GET操作中實例化一個練習。
public ActionResult Create(int workoutID)
{
Exercise ex= new Exercise();
ex.WorkOutId=workoutID;
ex.WorkOut=db.WorkOuts(workoutID);
return View(ex);
}
這看起來很糟糕,我在任何示例中都沒有看到類似的東西,但它似乎有效。同樣的練習對象會返回到我的POST創建動作
public ActionResult Create([Bind(Include = "Id,Name,WorkOutId")] Exercise exercise)
{
if (ModelState.IsValid)
{
db.Exercises.Add(exercise);
db.SaveChanges();
return RedirectToAction("Create", "RepUnits", new { exerciseId = exercise.Id });
}
return View(exercise);
}
您看到的將調用RepUnits控制器和關聯的Create操作。我做了一些非常相似的事情。創建一個代表對象,並將其傳遞給視圖,基本上我添加代表直到完成。最後,我將創建導航,以返回添加新練習或返回到索引視圖。
所以總結一下,傳遞整個對象似乎是浪費,也許我的整個實現是錯誤的,我應該試圖以某種方式在一個表單上完成這一切。至此谷歌搜索沒有找到我很多,因爲我不知道有什麼問題要問,但是這個帖子Creation of objects using form data within an ASP.NET MVC application剛剛彈出在類似的問題對話和應用程序是巧合非常相似!然而,當OP提到通過鍛鍊周圍時,這是如何同時發生的?我想也許使用ViewBag,但我如何得到視圖來處理這個ID? 我雖然嘗試,作爲一個例子
public ActionResult Create(int workoutId)
{
ViewBag.WoID = workoutId;
return View();
}
在ExercisesController
,然後在相應的創建視圖:
@Html.Hidden("WorkOutId", new { ViewBag.WoID })
但後來在視圖中,當我嘗試引用鍛鍊日期說到空白
<div class="form-group">
@Html.LabelFor(model => model.WorkOut.Date, "Work Out On:", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DisplayFor(model=>model.WorkOut.Date)
</div>
</div>
我應該做這樣的事情的看法: @ Model.WorkOutId = ViewBag.WoID;
哪些工作出於某種原因(編譯器錯誤信息:CS1525:無效的表達式'='),但是我是如何傳遞這些id的?
嘗試'@ {Model.Foo = ViewBag.Bar; },因爲你需要'@'來表示一個代碼塊而不是* print-here *操作符。 –
當我嘗試,我的表單中, @using(Html.BeginForm()){ @ Html.AntiForgeryToken() @ Html.Hidden( 「WorkOutId」,新{} ViewBag.WoID) @ { Model.WorkOutId = ViewBag.WoID; }我得到解析器錯誤消息:「@」字符後出現意外的「{」。 Outisde的形式,我得到一個空例外錯誤 – greedyLump