.NetCore MVC

这个是我自己记得笔记,最好有点基础看我的。

html 辅助标签

@Html.DropList

分布视图

使用 RenderPartialAsync 呈现分部视图。 此方法不返回 IHtmlContent。 它将呈现的输出直接流式传输到响应。 因为该方法不返回结果,所以必须在 Razor 代码块内调用它:微软推荐

@{

await Html.RenderPartialAsync("_AuthorPartial");

}

由于 RenderPartialAsync 流式传输呈现的内容,因此在某些情况下它可提供更好的性能。 在性能起关键作用的情况下,使用两种方法对页面进行基准测试,并使用生成更快响应的方法。

放在sharp下

JS,Css捆绑(.net5)

模型校验

模型上

【RegularExpression("")】

后端校验

可以·传参true排除属性错误。

模型加上后,label不用写labeltext。

@html.Raw(s.Description)正常渲染html字符串

模型绑定

【formform】表单提交post,form-data

不能写中文

layout布局

_ViewStatrt.cshtml

推荐使用标签助手代替html辅助标签

@addtaghelpers *,使Microsoft.AspNetCore.mvc.TagHelpers 类在我们所有 Razor 视图中可用

可以用了

<partial name="Shared/_ProductPartial.cshtml" for="Product">

model 属性分配模型实例,以传递到分部视图。model 属性不能与 for 属性一起使用。

在以下标记中,实例化新的 Product 对象并将其传递给 model 属性进行绑定:

  1. <partial name="_ProductPartial"
  2. model='new Product { Number = 1, Name = "Test product", Description = "This is a test" }'>
cs 复制代码
view-data 属性分配 ViewDataDictionary,以传递到分部视图。以下标记使整个 ViewData 集合可访问分部视图:

@{
    ViewData["IsNumberReadOnly"] = true;
}
<partial name="_ProductViewDataPartial" for="Product" view-data="ViewData">
在前面的代码中,IsNumberReadOnly 键值设置为 true 并添加到 ViewData 集合中。因此,在以下分部视图中可访问 ViewData["IsNumberReadOnly"]:

@model TagHelpersBuiltIn.Models.Product
<div class="form-group">
    <label asp-for="Number"></label>
    @if ((bool)ViewData["IsNumberReadOnly"])
    {
        <input asp-for="Number" type="number" class="form-control" readonly />
    }
    else
    {
        <input asp-for="Number" type="number" class="form-control" />
    }
</div>
<div class="form-group">
    <label asp-for="Name"></label>
    <input asp-for="Name" type="text" class="form-control" />
</div>
<div class="form-group">
    <label asp-for="Description"></label>
    <textarea asp-for="Description" rows="4" cols="50" class="form-control"></textarea>
</div>

自定义标签助手

cs 复制代码
// 作用于所有包含 "my-id" 属性的 <div> 标签

[HtmlTargetElement("div", Attributes = "my-id")]

// 作用于所有以 "my:" 为前缀的标签(如 <my:card>)
[HtmlTargetElement("", TagStructure = TagStructure.NormalOrSelfClosing, Prefix = "my")]

视图组件

传参

区域保持一致

mvc的视图注入在 ASP.NET Core 中将依赖项注入到视图 | Microsoft Learn

相关推荐
摇滚侠6 小时前
Spring MVC 不是一个单独的框架,是 Spring 框架的一个模块
java·spring·mvc
我登哥MVP1 天前
Spring Boot 从“会用”到“精通”:SpringBoot MVC 请求处理全流程
java·spring boot·后端·spring·mvc·maven·intellij-idea
摇滚侠3 天前
JavaWeb 全套教程 MVC 模式 93
mvc
代码的小搬运工3 天前
【iOS】MVC架构
ios·架构·mvc
qq_2518364575 天前
基于MVC的学校食堂点餐管理系统的设计与实现
mvc
故渊at5 天前
系列一:架构思想进阶 | 第1篇 Android 架构演进实录:从 MVC 的“万能类”到 MVVM 的数据驱动
android·架构·mvc
RR13356 天前
Spring MVC and Spring Gateway 的差异,以及报错处理
spring·gateway·mvc
gCode Teacher 格码致知6 天前
AspNet Mvc教学:Route路由切换-由Deepseek产生
mvc
鸠摩智首席音效师6 天前
如何在 macOS 上安装 .NET Core ?
macos·.netcore
gCode Teacher 格码致知7 天前
Asp.net Mvc教学: Url.Encode及Html.Encode的区别和联系-由Deepseek产生
asp.net·mvc