简体   繁体   English

ASP.NET MVC 3 在 cshtml 文件中使用 C# 代码

[英]ASP.NET MVC 3 use C# code in cshtml file

I'm a little new to ASP.NET MVC3.我对 ASP.NET MVC3 有点陌生。 I have this code in a cshtml file我在 cshtml 文件中有这段代码

    @grid.GetHtml(
        htmlAttributes: new { id = "grid" },
        tableStyle: "grid",
        headerStyle: "header",
        rowStyle: "row",
        footerStyle: "footer",
        alternatingRowStyle: "altRow",
        columns: grid.Columns(
            grid.Column(header: "", format: @<text><input name="Checked" type="checkbox" value="@item.Key" /></text>, style: "CheckboxColumn", canSort: true),
            grid.Column("Name", "Name"),
            grid.Column("Address", "Address"),
            grid.Column("City", "City"),
            grid.Column("PhoneNumber", "Phone Number"),
            grid.Column("", format: (item) =>
            {

                if (item.ID.Length > 0)
                {
                    //CODE GOES HERE
                    return Html.Raw(string.Format("<text><img src=\"{0}\" alt=\"Image\" class=\"ToolTip\" title=\"ID # {1} <br> \" /></text>", Url.Content("~/images/coupled.png"), @item.ID.ToString()));
                }
                else
                {
                    return Html.Raw("<text></text>");
                }
            }),
        ))

What I want is to write a C# Code at the //CODE GOES HERE section.我想要的是在//CODE GOES HERE部分写一个 C# 代码。 in order to change the Url.Content("~/images/coupled.png") epending on item ID.为了更改项目 ID 上的Url.Content("~/images/coupled.png")

So basically I want something like:所以基本上我想要这样的东西:

string URLOfPic;
if(item.ID > 1000)
{
    URLOfPic="~/images/aaa.png
}
else
{
    URLOfPic="~/images/bbb.png
}

and finally to use Url.Content(URLOfPic)最后使用Url.Content(URLOfPic)

So how can I use that C# code in the page?那么如何在页面中使用 C# 代码呢?

I hope I was clear.我希望我说得很清楚。 Thank you very much for any help非常感谢您的帮助

PS: I want it to be a C# code and not a javascript or anything else. PS:我希望它是 C# 代码而不是 javascript 或其他任何东西。 In normal ASP.NET I can just use the code behind to do it.在正常的 ASP.NET 中,我可以使用后面的代码来完成它。 but in MVC3 I have no idea how但在 MVC3 中我不知道如何

Being new to Asp.net mvc let me give you some advice, Don't Do That!作为 Asp.net mvc 的新手,让我给你一些建议,不要那样做! The most code you should have in a view is a for loop, all of that kind of logic should be done in the action.视图中最多应该有的代码是 for 循环,所有此类逻辑都应该在操作中完成。

basically take your data and do all logic and formatting in the action and add it to a view model then pass that to the view.基本上获取您的数据并在操作中执行所有逻辑和格式化并将其添加到视图 model 然后将其传递给视图。 Other wise your going to create very brittle code blocks that won't error until run time否则你将创建非常脆弱的代码块,直到运行时才会出错

Modify your item class to add an image class. In the image class you can add Image.URL, Image.Title.修改您的项目 class 添加图像 class。在图像 class 中,您可以添加 Image.URL,Image.Title。 This way in the controller you can assign the item an image based on it's ID.这样,在 controller 中,您可以根据项目的 ID 为项目分配图像。 This will also allow you to more easily modify the image URLs at a single point of code, instead of modifying in every view that you use this logic in.这也将允许您更轻松地在单个代码点修改图像 URL,而不是在您使用此逻辑的每个视图中进行修改。

I think you are close, did this not work?我认为你很接近,这没有用吗?

if (item.ID.Length > 0)
{
    string URLOfPic; 
    if(item.ID > 1000) 
    {     
        URLOfPic="~/images/aaa.png";
    } else {     
        URLOfPic="~/images/bbb.png";
    }
    return Html.Raw(string.Format("<text><img src=\"{0}\" alt=\"Image\" class=\"ToolTip\" title=\"ID # {1} <br> \" /></text>", Url.Content(URLOfPic), @item.ID.ToString()));                 
} 

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

相关问题 构建c#MVC asp.net api网页-我想使用index.cshtml页面,但收到失败消息:CS1980(代码说明请参见文本) - Building c# MVC asp.net api webpage- I want to use the index.cshtml page but get the failure message: CS1980 (see text for code description) 如何像在c#中的html解析器一样解析asp.net mvc剃刀视图(cshtml) - how parse asp.net mvc razor view (cshtml) like html parser in c# ASP.NET,MVC,C#应用程序-为不同的视图修改_Layout.cshtml - ASP.NET, MVC, C# application - modify _Layout.cshtml for different Views cshtml MVC ASP.NET之外的c#方法的正确位置 - correct position for c# methods outside from cshtml MVC ASP.NET 如何在 ASP.NET MVC 中使用另一个 cshtml 文件作为 SelectPDF 标头? - How to use another cshtml file as SelectPDF header in ASP.NET MVC? ASP.NET MVC4中cshtml文件中的解析错误 - Parsing error in cshtml file in ASP.NET MVC4 打开.aspx文件作为asp.net mvc中cshtml的部分视图 - Opening .aspx file as partial view of cshtml in asp.net mvc 如何在asp.net MVC上更改.cshtml文件的名称? - How to change name of .cshtml file on asp.net mvc? 如何重用.cshtml文件中定义的mvc asp.net函数? - How to reuse a mvc asp.net function defined in .cshtml file? ASP.NET MVC5:在浏览器中直接查看.cshtml文件 - ASP.NET MVC5: Directly view the .cshtml file in browser
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM