【问题标题】:Optimize web page styling with cshtml/c#/css/javascript使用 cshtml/c#/css/javascript 优化网页样式
【发布时间】:2023-04-10 15:10:01
【问题描述】:

我正在使用 cshtml/c#/css/javascript 的组合来创建一个网页,我是全新的,所以欢迎任何提示。

C# 文件创建一个部分类,这是一个示例 sn-p

[Required]
[Display(Name = "First Name *")]        //First Name * is displayed on actual page
public string FirstName{ get; set; }

cshtml 文件创建所有必要的容器、标签和段落。 CSS 包含所有样式。 javascript 增强了网页功能。

所以,这是一个交易:我需要添加 *,对于所有必需的元素,它应该都是红色的。我可以浏览所有可能的页面并手动将它们添加到每个元素,但我想是否可以优化代码。

是否可以在下面创建类似伪代码的东西?

if variable has a required flag set to it //from c# file
    add stylized red * next to the label 

任何帮助将不胜感激!

更新

我已经修改了我在下面提供的文章中的代码,但是,这是我一直遇到的另一个问题。这是我的助手修改后的代码:

public static MvcHtmlString EditorEntryFor<TModel, TValue>(
      this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression)
  {
     return BuildFormEntry(html.LabelWithRequiredMarkerFor(expression),
         html.EditorFor(expression), html.ValidationMessageFor(expression));
     //return BuildFormEntry(html.LabelWithRequiredMarkerFor(expression),
     //    html.TextAreaFor(expression), html.ValidationMessageFor(expression));
  }

private static MvcHtmlString LabelWithRequiredMarkerFor<TModel, TValue>(
      this HtmlHelper<TModel> html,
      Expression<Func<TModel, TValue>> expression)
  {
     var label = html.LabelFor(expression);
     if (ModelMetadata.FromLambdaExpression(expression, html.ViewData).IsRequired)
     {
        label = new MvcHtmlString(label.ToString().Substring(0, label.ToString().Length - 8).Trim() +
            "<span style=\"color:red\" class=\"required-marker\">*</span></label>");
     }
     return label;
  }

private static MvcHtmlString BuildFormEntry(
      MvcHtmlString label, MvcHtmlString input, MvcHtmlString validation)
  {
     return new MvcHtmlString("<div class=\"editor-label\">" + label + "</div>\n" +
     "<div class=\"editor-field\">" + input + validation + "</div>\n\n");
     //return new MvcHtmlString("<div class=\"editor-label\">" + label + "</div>\n");
  }

方法调用和LabelFor一模一样:

Html.EditorEntryFor(model => model.Project.FirstName)
//**equals to**
Html.LabelFor(model => model.Project.FirstName)

现在我希望 EditorEntryFor 中的表达式如下所示:

Html.EditorEntryFor(model => model.Project.FirstName, new { @title = "Enter firstname." })

但是,助手只接受单个参数表达式,我希望表达式值等于 model => model.Project.FirstName, new { @title = "Enter firstname." })

但是,Visual Studio 总是认为我尝试输入第二个参数。 有没有办法强制表达式认为“,”是表达式的一部分而不是第二个参数?到目前为止,看起来没有任何普通的字符串操作可以解决问题 =(

提前非常感谢大家!

【问题讨论】:

  • 是的,有很多库。示例见this
  • 您可以使用反射从类/属性中获取自定义属性并根据需要修改内容。
  • 如果您需要星号来标记必填字段,我在这里发现了类似的问题:stackoverflow.com/questions/4650637/…stackoverflow.com/questions/5940506/…。您需要的最重要的事情是拥有一些 CSS 选择器来设置必填字段的行为,例如 content: "*"; font-weight: bold; color: red
  • 非常感谢大家如此迅速的反应!我正在尽力首先检查是否没有重复此类问题,看来我仍然需要学习如何正确地进行研究。不过,我会研究所有三种可能性,并检查哪一种最适合我!
  • 除了已经提供的解决方案之外,我还发现了一些非常有用的文章,它们极大地帮助了我实现我所需要的:html helper with required fieldshtml helper

标签: javascript c# css razor computer-science


【解决方案1】:

除了已经提供的解决方案之外,我还发现了几篇非常有用的文章,它们极大地帮助了我实现所需的内容:html helper with required fieldshtml helper

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-13
    • 1970-01-01
    • 2021-10-29
    • 1970-01-01
    • 2020-03-05
    • 1970-01-01
    • 1970-01-01
    • 2011-06-24
    相关资源
    最近更新 更多