English 中文(简体)
• 如何利用这支气筒在MVC使用沥滤器展示数据
原标题:how do i use this linq query to display data using foreach loop in MVC

我不知道如何做到这一点。 我想用一个表格式显示数据。 这些数据将来自使用3-4表格和选择不同领域显示的圆环。 不知道如何回去,因为一度出现匿名错误。

var info = from s in context.LennoxSurveyResponses
                       join customers in context.Customers on s.SurveyCode equals customers.SurveyCode
                       join lists in context.CustomerLists on customers.ListId equals lists.ListId
                       join dealer in context.Channels on lists.ChannelId equals dealer.ChannelId
                       where dealer.ChannelId == id
                       select new
                       {
                           ResponseId = s.ResponseId,
                           CustomerFirstName = customers.FirstName,
                           CustomerLastName = customers.LastName,
                           CustomerTestimonial = s.Question5Answer
                       };

我的模式必须宣布什么,才能通过?

最佳回答

这一观点符合观点模式,因此首先界定:

public class CustomerViewModel
{
    public int ResponseId { get; set; }
    public string CustomerFirstName { get; set; }
    public string CustomerLastName { get; set; }
    public string CustomerTestimonial { get; set; }
}

之后:

public ActionResult Foo(string id)
{
    var info = 
        from s in context.LennoxSurveyResponses
        join customers in context.Customers on s.SurveyCode equals customers.SurveyCode
        join lists in context.CustomerLists on customers.ListId equals lists.ListId
        join dealer in context.Channels on lists.ChannelId equals dealer.ChannelId
        where dealer.ChannelId == id
        select new CustomerViewModel
        {
            ResponseId = s.ResponseId,
            CustomerFirstName = customers.FirstName,
            CustomerLastName = customers.LastName,
            CustomerTestimonial = s.Question5Answer
        };
    return View(info);
}

并且最后,你的意见被严格分类如下:

@model IEnumerable<CustomerViewModel>

现在,您可以loop使用一个显示模板,该模型向用户提供信息:

@Html.DisplayForModel()

以及相应的显示模板(~/Views/Home/DisplayTemplates/CustomerViewModel.cshtml):

@model CustomerViewModel
<div>
    <span>First name: @Html.DisplayFor(x => x.CustomerFirstName)</span>
    <span>Last name: @Html.DisplayFor(x => x.CustomerLastName)</span>
    ...      
</div>
问题回答

暂无回答




相关问题
Using jquery to get a partial view and updating the UI

I need to render a partial view (returned from the controller) to show some customer summary details. This will need to happen when the user clicks on a button. In the the mean time the user can ...

MVC 2 / MVC 3 / MVC 4

MVC 2 我们可以轻松地创造领域。 现在,我的问题涉及nes地区(地区内)。

Asp.Net MVC 2 - Changing the PropertyValueRequired string

Using a resx file in the App_GlobalResources directory, I ve been able to change the default message for the PropertyValueInvalid string of the model validators. But it doesn t work to translate the ...

ASP.NET MVC 3 - What features do you want to see? [closed]

I know a bunch of people that are really enjoying the improvements that ASP.NET MVC 2 made over the first release. I have just started to migrate our MVC 1 project over and so far areas has totally ...

热门标签