一个表单涉及多个数据表 [英] Multiple data tables involved on one form

查看:116
本文介绍了一个表单涉及多个数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新08/14/2012 12:05 pm
我会尝试解释我的情况,希望有人能指出我正确的方向。

Updated 08/14/2012 12:05pm I will try to explain my situation and hopefully someone can point me in the right direction.

我在我的项目中有一个表单将包含几个表,所有的表都已经建立了关系。以下是相关的模型。

I have a form in my project that will have several tables involved all of the tables are already set up with relationships. Here are the models that are related.

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:

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; }

    }
 }

我有一个困难的时刻找出如何从一个与另一个表关系的表中显示一个问题,其中包含子问题和答案的复选框。答案很简单是,否,不适用。他们也在不同的桌子上。我在数据表中拥有所有这些的原因是它们必须是可编辑的,或者我想添加到这些区域。
这是页面应该是什么样子。

I am having a difficult time figuring out how to display a question from one table that has a relationship with another table with sub questions and check boxes for the answer. The answers are simple Yes, No, and N/A. They are also held in a different table. The reason i have all of this in data tables is they have to be editable or if i want to add to the areas. This is what the page should look like. i have incerted text to give the look for the page.

布局:

<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>

这是它的样子:

This is what it looks like:

我已经在global.asax中设置了一个字符串来接受审计记录然后拉主要问题。有一个侧面菜单将选择问题区域。这是在审核时间表是身体和侧面菜单,并且表单问题/答案是部分的Html.Action()的视图中。

I have set up in the global.asax a string to accept a audit record and then pull the main question. there is a side menu that will select the question area's. This is done in a view where the Audit schedule is the Body and the side menu and form question/answers are Html.Action() for partials.

全局代码:

            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#编码中的数据库我真的很弱。

I have tried to play around with a view model for this but i cannot get this to work. When it comes to databases in C# coding i am really weak.

这是我正在使用控制器的模型:

Here is the Model i am working on with the controller:

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);
        }
    }

正如我在ViewModel不工作之前所说的,因为我我要求一个不存在的id。现在,视图模型只有代码来填充复选框的答案。

As i stated before the ViewModel does not work because i am asking for an id that does not exist. Right now the view model only has code to populate check boxes for the answers.

添加视图代码08/14/2012 1:00 pm

Added view codes 08/14/2012 1:00pm

形式部分视图大多是测试,但脚本赞美复选框代码

Form partial view mostly test but script that compliments the check box code

@{ 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") &nbsp;
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") &nbsp;
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>

这是侧面菜​​单部分:

Here is the side menu partial:

@{ 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 />

}


推荐答案

为什么你添加一个 int id 参数到_Forms HttpPost动作?你不使用它,所以你不需要它,对吧?您也不会演示您的视图代码。

why are you adding an int id parameter to _Forms HttpPost action? You don't use it so you don't need it then, right? You also don't demonstrate your view code.

这篇关于一个表单涉及多个数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆