简体   繁体   English

For Loop ASP.NET MVC3中的选定列

[英]Selected column in For Loop ASP.NET MVC3

I have a following function in a controller. 我在控制器中具有以下功能。 It is called from index.cshtml . index.cshtml var relays works fine and I got the replays.count() . var relays工作正常,我得到了replays.count() PRESETDETAILS has some columns from which I want to access specific columns in a loop (like I have mentioned in response.write ). PRESETDETAILS有一些列,我想从中访问循环中的特定列(就像我在response.write提到的那样)。

Kindly tell me how I can get the specific columns in the for loop. 请告诉我如何获取for循环中的特定列。

    #region "Apply Preset Handlers"
    public ActionResult btnApply_Click(int? id)
    {
        var relays = db.PRESETDETAILS.ToList().Where(t => (t.PRESETID).Equals(id));
        for (int k = 0; k < relays.Count(); k++)
        {
            Response.Write(relays.Select(s => new relays{PRESETDETAILID = s.PRESETDETAILID }).ToString());   

        }
        return View("Index");
    }
    #endregion

you need to loop through them, you're simply select the same things over and over... 您需要遍历它们,您只需一遍又一遍地选择相同的内容...

var relays = db.PRESETDETAILS.Where(t => t.PRESETID == id).ToList();
foreach (var replay in replays)
{
    Response.Write(string.Format("{0}", relay.ID));   
}

NOW... looking at your code: 现在...查看您的代码:

  • always use ToList() at the end of the query; 在查询末尾始终使用ToList()
  • ToList() actually makes the call to the database, until then, it's just a promise ToList()实际上是对数据库的调用,直到那时,这只是一个承诺
  • Don't use Response.Write in your Controller, send the data to the View instead 不要在Controller中使用Response.Write ,而是将数据发送到View

your code should be: 您的代码应为:

public ActionResult btnApply_Click(int? id)
{
    var model = db.PRESETDETAILS.Where(t => t.PRESETID == id).ToList();
    return View(model);
}

in your View you can loop through the records 在您的视图中,您可以遍历记录

@model List<YOUR_CLASS>

<ul>
    @foreach(var replay in Model)
    {
        <li>@replay.ID</li>
    }
</ul>

I can see that some MVC conventions are not yet in place with you, and I know it can be somewhat overwhelm from someone that comes from WebForms or a linear code approach, but why don't you take some time and check the FREE available course about ASP.MVC in the ASP.NET Website? 我可以看到某些MVC约定尚未实现,并且我知道它可能会有点不符合WebForms或线性代码方法的要求,但是为什么不花一些时间检查免费的可用课程ASP.NET网站中有关ASP.MVC的信息?

See the videos in the right side of this page: http://www.asp.net/mvc 请参阅此页面右侧的视频: http : //www.asp.net/mvc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM