更新時間2012年8月14日下午12:05 我會盡力解釋我的情況,希望有人能指引我朝着正確的方向前進。一個表格涉及多個數據表
我在我的項目中有一個表格,它將包含幾個表格,所有的表格都已經建立了關係。以下是相關的模型。
namespace QQAForm.Models
{
public class AuditSchedule
{
public virtual int AuditScheduleID { get; set; }
public virtual Nullable<DateTime> audit_completed_date { get; set; }
public virtual string gl_cmp_key { get; set; }
public virtual string audit_year { get; set; }
public virtual string ar_ship_key { get; set; }
public virtual string ar_ship_name { get; set; }
public virtual string im_adres_city { get; set; }
public virtual string im_adres_state { get; set; }
public virtual string audit_type { get; set; }
public virtual string audit_no { get; set; }
public virtual string audit_group { get; set; }
public virtual string Footage { get; set; }
public virtual string Rolling3MosFootage { get; set; }
public virtual string snp_SalesRep8 { get; set; }
public virtual string epg_sales_rep_accountable { get; set; }
public virtual string tech_service_rep { get; set; }
public virtual string audit_done_by { get; set; }
public virtual Nullable<DateTime> audit_recieved_date { get; set; }
public virtual string audit_notes { get; set; }
public virtual string audit_pdf { get; set; }
public virtual string updated { get; set; }
public virtual string hidden { get; set; }
public virtual string en_stats_key { get; set; }
public virtual string Control_ID { get; set; }
public virtual Nullable<DateTime> audit_date { get; set; }
public virtual string contacts_present { get; set; }
public virtual string audit_furnished_to { get; set; }
public virtual string spacer_type { get; set; }
}
}
MainQuestion:
namespace QQAForm.Models
{
public class MainQuestion
{
public virtual int MainQuestionID { get; set; }
public virtual int SubCategoryID { get; set; }
public virtual int ReferenceNo { get; set; }
public virtual int DisplayIndex { get; set; }
public virtual int SuggestionID { get; set; }
public virtual string Question { get; set; }
public virtual SubCategory SubCategory { get; set; }
public virtual ICollection<Suggestion> suggestions { get; set; }
public virtual ICollection<DocumentLink> documentLink { get; set; }
}
}
兒童問題:
namespace QQAForm.Models
{
public class ChildQuestion
{
public virtual int ChildQuestionID { get; set; }
public virtual int MainQuestionID { get; set; }
public virtual int ReferenceNo { get; set; }
public virtual int DisplayIndex { get; set; }
public virtual string QuestionText { get; set; }
public virtual string UserEntityType { get; set; }
public virtual string UserEntityTexts { get; set; }
public virtual MainQuestion MainQuestion { get; set; }
}
}
建議:
namespace QQAForm.Models
{
public class Suggestion
{
public virtual int SuggestionID { get; set; }
public virtual int MainQuestionID { get; set; }
public virtual int DisplayIndex { get; set; }
public virtual string ReferenceNo { get; set; }
public virtual string Suggestions { get; set; }
public virtual MainQuestion MainQuestion { get; set; }
}
}
主要答:
namespace QQAForm.Models
{
public class MainAnswer
{
public virtual int MainAnswerID { get; set; }
public virtual int AuditScheduleID { get; set; }
public virtual int MainQuestionID { get; set; }
public virtual string Score { get; set; }
public virtual string AdditionalNotes { get; set; }
public virtual AuditSchedule AuditSchedule { get; set; }
public virtual MainQuestion MainQuestion { get; set; }
}
}
我很難搞清楚如何顯示一個表格中的問題,這個問題與另一個帶有子問題的表格有關係,並且複選框顯示答案。答案很簡單是,不,和不適用。他們也被放在另一張桌子上。我在數據表中擁有所有這一切的原因是他們必須是可編輯的,或者如果我想添加到區域。 這是該頁面的外觀。我已經觸發了文字來尋找頁面。
佈局:
<html>
<head>
<title>@ViewBag.Title</title>
<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"> </script>
</head>
<body>
<div id="head" class="container">
@Html.Partial("Header")
</div>
<div class="container">
<div id="main">
<ul id="breadcrumbs">
</ul>
<div id="formTopCol">
@RenderBody()
@*@Html.Partial("_Audit")*@
</div>
<div id="formBottomCol">
@Html.Action("_SubCategory")
<div id="formBottomRightCol">
@Html.Action("_Forms")
@*@RenderBody()*@
</div>
<div style="clear:left;"></div>
</div>
</div>
</div>
<div class="container">
@Html.Partial("Footer")
</div>
</body>
</html>
這是什麼樣子:
我已經在Global.asax設立一個字符串接受審計記錄,然後拉的主要問題。有一個側面菜單可以選擇問題區域。這是在審計時間表是主體和側面菜單並且表單問題/答案是Html.Action()來進行的情況下完成的。
全球守則:
routes.MapRoute(
"AuditSchedule", // Route name
"AuditSchedule/Audit/{id}/{section}", // URL with parameters
new { controller = "AuditSchedule", action = "Audit", id = UrlParameter.Optional, section = UrlParameter.Optional } // Parameter defaults
);
我試圖玩弄這個視圖模型,但我不能得到這個工作。說到C#編碼的數據庫,我真的很虛弱。
這裏是我與控制器工作的模式:
namespace QQAForm.ViewModels
{
public class AuditFormEdit
{
public Models.MainAnswer ScoreInstance { get; set; }
public List<ScoreCardCheckBoxHelper> ScoreCardCheckBoxHelperList { get; set; }
public void InitializeScoreCheckBoxHelperList(List<Models.Score> ScoreList)
{
if (this.ScoreCardCheckBoxHelperList == null)
this.ScoreCardCheckBoxHelperList = new List<ScoreCardCheckBoxHelper>();
if (ScoreList != null
&& this.ScoreInstance != null)
{
this.ScoreCardCheckBoxHelperList.Clear();
ScoreCardCheckBoxHelper scoreCardCheckBoxHelper;
string scoreTypes =
string.IsNullOrEmpty(this.ScoreInstance.Score) ?
string.Empty : this.ScoreInstance.Score;
foreach (Models.Score scoreType in ScoreList)
{
scoreCardCheckBoxHelper = new ScoreCardCheckBoxHelper(scoreType);
if (scoreTypes.Contains(scoreType.ScoreName))
scoreCardCheckBoxHelper.Checked = true;
this.ScoreCardCheckBoxHelperList.Add(scoreCardCheckBoxHelper);
}
}
}
public void PopulateCheckBoxsToScores()
{
this.ScoreInstance.Score = string.Empty;
var scoreType = this.ScoreCardCheckBoxHelperList.Where(x => x.Checked)
.Select<ScoreCardCheckBoxHelper, string>(x => x.ScoreName)
.AsEnumerable();
this.ScoreInstance.Score = string.Join(", ", scoreType);
}
public class ScoreCardCheckBoxHelper : Models.Score
{
public bool Checked { get; set; }
public ScoreCardCheckBoxHelper() : base() { }
public ScoreCardCheckBoxHelper(Models.Score scoreCard)
{
this.ScoreID = scoreCard.ScoreID;
this.ScoreName = scoreCard.ScoreName;
}
}
}
}
控制器部分:
//get
public ActionResult _Forms(int id)
{
AuditFormEdit viewModel = new AuditFormEdit();
//viewModel.ScoreInstance = _db.MainAnswers.Single(r => r.AuditScheduleID == id);
viewModel.InitializeScoreCheckBoxHelperList(_db.Scores.ToList());
return View(viewModel);
}
//post
[HttpPost]
public ActionResult _Forms(int id, AuditFormEdit viewModel)
{
if (ModelState.IsValid)
{
viewModel.PopulateCheckBoxsToScores();
_db.Entry(viewModel.ScoreInstance).State = System.Data.EntityState.Modified;
_db.SaveChanges();
return RedirectToAction("/");
}
else
{
return View(viewModel);
}
}
正如我前面所說的視圖模型不能正常工作,因爲我要求中的標識不存在。現在,視圖模型只有代碼來填充答案的複選框。
致意複選框碼@{ Layout = null; }
@model QQAForm.ViewModels.AuditFormEdit
<table width="698" border="2" cellpadding="2">
<tr>
<td align="center"><b>Section</b><br />1.0</td>
<td><b>Glass edge damage noted. (shells, flakes, sharks teeth)</b>
<br /><br />
@Html.CheckBox("suggestion1")
It was noted that there was a significant amount of glass edge damage observed on the IG units being produced.
This glass edge damage may lead to a significantly high glass breakage rate in IG unit handling, in the glazing operation and in service.
[MAJOR CONCERN]
The cause of this glass edge damage should be determined and efforts made to eliminate the damage.
<br /><br />
@Html.CheckBox("suggestion2")
The glass edge should be smooth and free of chips, flakes, wings, or other damage. Damaged edges may result in stress cracks or premature IG unit failure.
[MAJOR CONCERN]
<br /><br />
<label>Additional Notes:</label><br />
@Html.TextArea("Additional Notes")
<br />
</td>
<td>
@for (int index = 0; index < Model.ScoreCardCheckBoxHelperList.Count; index++)
{
@Html.CheckBoxFor(m => m.ScoreCardCheckBoxHelperList[index].Checked)
@Html.LabelFor(m => m.ScoreCardCheckBoxHelperList[index], Model.ScoreCardCheckBoxHelperList[index].ScoreName)
@Html.HiddenFor(m => m.ScoreCardCheckBoxHelperList[index].ScoreID)
@Html.HiddenFor(m => m.ScoreCardCheckBoxHelperList[index].ScoreName)
}
</td>
</tr>
</table>
這裏
添加視圖代碼08/14/2012下午1:00
形式的局部視圖主要是試驗,但劇本是邊菜單部分:
@{ Layout = null; }
@model IEnumerable<QQAForm.Models.SubCategory>
<div id="menuCol">
<h3>Audit Sections</h3>
<ul>
<li>
@foreach (var item in Model)
{
<div class="sidemenu">
@Html.ActionLink(item.SubcategoryName, "Audit", new { section = item.SubCategoryID }, null)
</div>
}
</li>
</ul>
</div>
這是正文:
@model QQAForm.Models.AuditSchedule
@{
ViewBag.Title = "Edit";
Layout = "~/Views/AuditSchedule/_FormLayout.cshtml";
}
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
<table class="audit-display">
<tr>
<th>ID</th>
<th>Customer</th>
<th>City</th>
<th>State</th>
<th>EPG TSM Rep/NAM</th>
<th>EPG RSM (or NAM's VP)</th>
<th>Tech Rep</th>
<th>Audit Type</th>
</tr>
<tr>
<td>@Html.DisplayFor(m => m.AuditScheduleID)</td>
<td>@Html.DisplayFor(m => m.ar_ship_name)</td>
<td>@Html.DisplayFor(m => m.im_adres_city)</td>
<td>@Html.DisplayFor(m => m.im_adres_state)</td>
<td>@Html.DisplayFor(m => m.epg_sales_rep_accountable)</td>
<td>@Html.DisplayFor(m => m.snp_SalesRep8)</td>
<td>@Html.DisplayFor(m => m.tech_service_rep)</td>
<td>@Html.DisplayFor(m => m.audit_type)</td>
</tr>
</table>
<table class="audit-display">
<tr>
<th>Date</th>
<th>Contacts Present</th>
<th>Audit Furnished To</th>
<th>Audit Done By (If not sheduled)</th>
<th>Spacer's Used</th>
</tr>
<tr>
<td>@Html.DisplayFor(m => m.audit_date)</td>
<td>@Html.DisplayFor(m => m.contacts_present)</td>
<td>@Html.DisplayFor(m => m.audit_furnished_to)</td>
<td>@Html.DisplayFor(m => m.audit_done_by)</td>
<td>@Html.DisplayFor(m => m.spacer_type)</td>
</tr>
</table>
</fieldset>
<hr />
}
你的問題是不可讀的。嘗試更簡潔,添加段落,縮進,代碼示例... – 2012-08-14 15:24:06
我已將所有相關的代碼添加到問題中。 – 2012-08-14 15:59:02
是否有拋出異常? – Alex 2012-08-14 16:13:48