在 MVC5 中以部分视图提交表单时模型为空

2023-12-01

我只是想学习 MVC 并面临一些问题。当我提交我的部分视图时,我在模型中得到 nullBlog在创建方法内。

我做错了什么以及正确的方法是什么?

查看(索引.cshtml)

@model IEnumerable<Samples.Controllers.Blog>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>Sample</th>
        <th>URL</th>
        <th>Name</th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
            @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
            @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
        </td>
        <td>
            @item.URL
        </td>
        <td>
            @item.Name
        </td>
    </tr>
}

</table>

@Html.Partial("_CreateBlog", new Samples.Controllers.Blog())

部分视图(_CreateBlog.cshtml)

@model Samples.Controllers.Blog

@using (Html.BeginForm("Create","Sample",FormMethod.Post)) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Blog</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="row">
            @Html.LabelFor(model => model.URL)
            @Html.EditorFor(model => model.URL)
        </div>
        <div class="row">
            @Html.LabelFor(model => model.Name)
            @Html.EditorFor(model => model.Name)
        </div>
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

示例控制器.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Samples.Controllers
{
    public class SampleController : Controller
    {
        List<Blog> lstBlogs;
        public SampleController() 
        {
            lstBlogs = new List<Blog> 
           {
               new  Blog{ Name="Domnic", URL=   "www.google.com"},
               new  Blog{ Name="Tom", URL=   "www.YAHOO.com"},
               new  Blog{ Name="Cat", URL=   "www.facebook.com"},
               new  Blog{ Name="Bob", URL=   "www.twitter.com"}
           };
        }
        // GET: Sample
        public ActionResult Index()
        {
            return View(lstBlogs);
        }
        public ActionResult IndexWithDynamicView()
        {
            return View(lstBlogs);
        }

        [HttpPost]
        public void Create(Blog blog)
        { 

        }
    }
    public class Blog
    {
        public string Name;
        public string URL;
    }

}

你的班Blog只包含字段,不包含属性,因此DefaultModelBinder cannot set他们的价值观。更改它添加 getter/setter

public class Blog
{
    public string Name { get; set; }
    public string URL { get; set; }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 MVC5 中以部分视图提交表单时模型为空 的相关文章

随机推荐

  • NUL 和 NULL 有什么区别? [复制]

    这个问题在这里已经有答案了 NULL是一个定义在的宏
  • 如何在 Kusto 中将 JSON 转换为键值表

    我有一张由一行和多列组成的表格 其中一列被命名为EventProperties这是以下格式的属性的 JSON Success true Counters Counter1 1 Counter2 1 Counter3 5 Counter4 4
  • Spring 3.1 MVC 应用程序上的 HTTP 状态 404

    我正在尝试创建并运行一个简单的 Spring 3 1 mvc Web 应用程序 其中我定义了一个控制器 该控制器使用以下类在响应正文中简单地返回 hello package com jr freedom controllers import
  • 如何使用 Gerrit 进行分支审查

    如何使用 gerrit 进行分支审查 我想分析一次 gerrit 更改中的所有分支更改 这个想法是审查具有多个更改 提交 的分支的代码 如果您想审查具有多个提交的分支 Gerrit 不是一个合适的工具 Gerrit 中的审核是在每次提交中单
  • 是否可以在 GlassFish 3.1 上为无状态 ejb web 服务定义 servlet 过滤器

    我想导出一个作为无状态 EJB 实现的 Web 服务 我知道这些 WebServices 是由 EJB 容器处理的 当它们被注释为 Stateless Webservice 时 是否可以通过 Servlet Filter 将所有传入请求路由
  • 将未知的装箱简单值类型(char、int、ulong 等)转换为 UInt64

    扩展乔恩 斯基特的答案上一个问题 Skeet 没有解决负值和二进制补码出现时发生的故障 简而言之 我想转换任何简单类型 保存在未知的盒装中 object to System UInt64所以我可以使用底层的二进制表示 我为什么要这样做 请参
  • 如何从 React 制作 PDF?

    我将在 React JS 中使用 jsPDF 库 但出现错误 如果有人收到我的查询 请告诉我 我已经尝试了两天多 但我做不到 Step1 包 json 依赖关系 jspdf git github com MrRio jsPDF 76edb3
  • onLoadFinished 触发时页面未完全加载/渲染

    我正在使用 Phantomjs 来检查我的应用程序 我正在查看的页面包含很多组件 并且由 Angularjs 提供支持 我的测试服务器很慢 在里面onLoadFinished事件 我正在使用渲染对网页进行成像 page onLoadFini
  • Access DB事务插入限制

    在需要提交之前或 Access Jet 引发错误之前 Access 事务中可以执行的插入数量是否有限制 我当前正在运行以下代码 希望确定这个最大值是多少 OleDbConnection cn new OleDbConnection Prov
  • 如何在Linux内核空间中打印用户空间堆栈跟踪

    比如说 我挂接了一个 sys 例如 sys open 处理程序 当我发现从用户空间传递的参数是恶意的时 我打印用户空间的堆栈跟踪 我该怎么做 dump stack 仅打印内核堆栈 您可以向内核中的目标进程发送信号 例如 SIGBUS SIG
  • python mss mss.exception.ScreenShotError:

    我是新手 我尝试使用 mss 来截图监视器 我的代码 for i in range 1 20000 cactus box left 508 top 382 width 30 height 33 sct mss sct img sct gra
  • 有没有办法从 Laravel 中急切加载的表中选择字段?

    所以我在下面定义了几个关系表 父数据库 public function backorderQuantities return this gt hasMany BackorderQuantity class ITEMNMBR ITEMNMBR
  • 检查矩阵的每一行是否在另一个矩阵中 [Python]

    我有两个矩阵 或者更好的是 二维 numpy 数组 A and B 具有相同的列数 公平地说 它们具有不同的行数 但是我只是将两个矩阵切成两列 但行数不同 我想检查是否每一行A is in B以及正确的行索引是什么B 我实际上正在使用常规循
  • ASP.NET 中内联标记的全球化差异

    使用内联写入标签而不是资源标签有什么优点 缺点吗 例子 在任何 MSDN 示例中都可以看到资源标签 表达式标签
  • 为什么 margin:auto 不足以使位置绝对或固定居中?

    我试图将 div 的内容居中 我的父容器设置为 相对 我在内部 div 上使用它 position absolute margin auto width 70px height 70px 但它拒绝居中 我不得不添加左侧和右侧的0 但我不明白
  • 查找具有所有关联记录的记录

    Say 我们有 个人 和 最喜欢的 模型 最喜欢的 是这个人喜欢的东西 音乐 视频 运动 互联网 旅行 等 人物 HABTM 收藏夹 和 收藏夹 HABTM 人物 我需要找到一个人 它列出了所有 最爱 例如 找到一个人 喜欢 音乐 旅行 和
  • 为什么 Python 集合不可散列?

    我偶然发现一篇博客文章 详细介绍了如何在 Python 中实现幂集函数 所以我开始尝试自己的方法 发现Python显然不能有一组集合 因为集合不可散列 这很令人厌烦 因为幂集的定义是它是一组集合 而我想使用实际的集合运算来实现它 gt gt
  • 系数表在秩缺陷拟合中没有 NA 行;如何插入它们?

    library lmPerm x lt lmp formula a b c d e data df perm Prob summary x truncated output I can see NA rows here Coefficien
  • Django 模板语法错误

    下面的代码语法有问题吗 有错误Invalid block tag else ifequal chat profile 1 extends chatprofile chat profile1 html else extends chatpro
  • 在 MVC5 中以部分视图提交表单时模型为空

    我只是想学习 MVC 并面临一些问题 当我提交我的部分视图时 我在模型中得到 nullBlog在创建方法内 我做错了什么以及正确的方法是什么 查看 索引 cshtml model IEnumerable