Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在MVC中用颜色编码区分从数据库加载的问题_C#_Html_Entity Framework_Model View Controller - Fatal编程技术网

C# 在MVC中用颜色编码区分从数据库加载的问题

C# 在MVC中用颜色编码区分从数据库加载的问题,c#,html,entity-framework,model-view-controller,C#,Html,Entity Framework,Model View Controller,我正在将所有问题加载到模型中,然后加载到数据库中 我对问题的颜色编码有问题。 我在数据库中有一个表,我从中获取所有问题 这就是我正在使用的方法 public List<QuestionAnswerModel> GetQuestionsbyAppId(int appId, string purpose = "XXX") { using (var context = new someEntities()) {

我正在将所有问题加载到模型中,然后加载到数据库中

我对问题的颜色编码有问题。 我在数据库中有一个表,我从中获取所有问题

这就是我正在使用的方法

  public List<QuestionAnswerModel> GetQuestionsbyAppId(int appId, string purpose = "XXX")
        {
            using (var context = new someEntities())
            {
                var data = (from q in context.Questions
                            join aq in context.Application_Question_Mapping.Where(a => a.ApplicationId == appId) on q.QuestionId equals aq.QuestionId into answer
                            from an in answer.DefaultIfEmpty()
                            where q.Purpose == purpose
                            select new QuestionAnswerModel
                            {
                                QuestionId = q.QuestionId,
                                QuestionText = q.QuestionText,
                                SelectedAnswer = an == null ? String.Empty : an.Answer,
                                QuestionOptions = q.QuestionOptions,
                                Comments = (an == null) ? string.Empty : an.Comments,
                                AnsweredBy = an == null ? null : an.AnsweredBy,
                                AnswerDateTime = an == null ? null : an.AnsweredTime,
                            }).ToList();

                return data;
            }
        }
公共列表GetQuestionsbyAppId(int-appId,string-purpose=“XXX”) { 使用(var context=new someEntities()) { var数据=(来自上下文中的q。问题 在context.Application\u Question\u映射中加入aq,其中q.QuestionId上的(a=>a.ApplicationId==appId)等于aq.QuestionId进入答案 来自in-answer.DefaultIfEmpty()中的 其中q.目的==目的 选择新的问题回答模型 { QuestionId=q.QuestionId, QuestionText=q.QuestionText, SelectedAnswer=an==null?字符串。空:an.Answer, QuestionOptions=q.QuestionOptions, Comments=(an==null)?string.Empty:an.Comments, AnsweredBy=an==null?null:an.AnsweredBy, AnswerDateTime=an==null?null:an.AnsweredTime, }).ToList(); 返回数据; } } 在HTML中,我将问题显示为

 @foreach (var question in Model)
    {
        <div class="whiteContainer pad-10 pull-left col-lg-15" style="width: 95%; padding: 5px; margin-bottom: 3px">
            <div class="form-group">
                <input type="hidden" value="@question.QuestionId" name="QuestionId" id="QuestionId" />
                <label class="label-reports">@question.QuestionText</label>
                <br>
                @foreach (var ans in question.QuestionOptions.Split('|'))
                {
                    if (!string.IsNullOrWhiteSpace(question.SelectedAnswer) && ans == question.SelectedAnswer)
                    {
                        <label class="radio-inline">
                            <input type="radio" @((ViewBag.UserId != null && ViewBag.UserId == question.AnsweredBy) || (question.AnsweredBy == null) ? "" : "disabled") checked="checked" name="@string.Format("inlineRadioOptions-{0}", question.QuestionId)" id="@string.Format("{0}{1}", question.QuestionId, question.SelectedAnswer)" value="@ans"> @ans
                        </label>
                    }
                    else
                    {
                        <label class="radio-inline">
                            <input type="radio" @((ViewBag.UserId != null && ViewBag.UserId == question.AnsweredBy) || (question.AnsweredBy == null) ? "" : "disabled") name="@string.Format("inlineRadioOptions-{0}", question.QuestionId)" id="@string.Format("{0}{1}", question.QuestionId, question.SelectedAnswer)" value="@ans"> @ans
                        </label>
                    }
                }
                <br />
                <label class="label-reports" >Comments</label>
                <input @((ViewBag.UserId != null && ViewBag.UserId == question.AnsweredBy) || (question.AnsweredBy == null) ? "" : "disabled") maxlength="100" type="text" class="form-control" style="width: 900px !important" for="comments" value="@question.Comments" />
            </div>

        </div>
    }
@foreach(模型中的变量问题)
{
@问题,问题文本

@foreach(var-ans有问题。QuestionOptions.Split(“|”)) { 如果(!string.IsNullOrWhiteSpace(question.SelectedAnswer)&&ans==question.SelectedAnswer) { @ans } 其他的 { @ans } }
评论 }
通过这个,我可以得到所有的问题,现在的问题是我必须对一些问题进行颜色编码(比如第一个问题和第十个问题)

谁能提出一个方法来做这件事


谢谢

关于如何确定问题是否为不同颜色的问题,目前还不清楚。然而,根据OP:

“类似于说他们希望前5个问题为红色,下5个问题为蓝色,我有一个来自db表的
questionid
,基于他们我想给它上色”

记住这一点,你可以简单地在剃须刀中放入
if
语句来确定颜色

@if (question.questionId <= 5)
{
    <div class="redContainer pad-10 pull-left col-lg-15" style="width: 95%; padding: 5px; margin-bottom: 3px">
}
else
{
    <div class="blueContainer pad-10 pull-left col-lg-15" style="width: 95%; padding: 5px; margin-bottom: 3px">
}

@if(question.questionId您需要返回给您说明的人,告诉他们问题模型中需要有另一个属性,可以用来确定颜色


如果将问题4、5和6涂成红色,当有人在数据中的上方或之间插入新问题时会发生什么情况?您必须更改代码!应该完成的代码,忘记了,再也不需要更改了!

颜色编码的决定因素是什么?您可以在
QuestionAnswerModel
对象上添加属性,然后把它作为循环中div的颜色,或者你可以在视图中做一个简单的if语句,条件是确定它应该是什么颜色。我希望2-8中所有问题的颜色都是红色为什么这里有一个场景,我应该用不同的颜色显示特定的问题如果你有16个问题,那么它们都是完全相同的问题,你仍然希望问题2-8是红色的吗?不,它们不是完全相同的问题,它们不同,它们之间只有一些特殊的问题,我需要用颜色编码,因为它们需要以不同的颜色显示,因为它们比其他颜色更重要。这就是我所做的。谢谢你的帮助。