在MVC5中以局部视图提交表单时模型为空 [英] Model is null while submitting the form in partial view in MVC5

查看:37
本文介绍了在MVC5中以局部视图提交表单时模型为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想学习 MVC 并面临一些问题.当我提交我的部分视图时,我在 Create Method 内的模型 博客 中得到空值.

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

查看(Index.cshtml)

@model IEnumerable@{ViewBag.Title = "索引";}<h2>索引</h2><p>@Html.ActionLink("新建", "创建")</p><table class="table"><tr><第>样本</th><th>URL</th><th>姓名</th></tr>@foreach(模型中的变量项目){<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>@项目名称</td></tr>}@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-horizo​​ntal"><h4>博客</h4><小时/>@Html.ValidationSummary(true, "", new { @class = "text-danger" })<div class="row">@Html.LabelFor(model => model.URL)@Html.EditorFor(model => model.URL)

<div class="row">@Html.LabelFor(model => model.Name)@Html.EditorFor(model => model.Name)

<div class="form-group"><div class="col-md-offset-2 col-md-10"><input type="submit" value="Create" class="btn btn-default"/>

}<div>@Html.ActionLink("返回列表", "索引")

SampleController.cs

使用系统;使用 System.Collections.Generic;使用 System.Linq;使用 System.Web;使用 System.Web.Mvc;命名空间 Samples.Controllers{公共类 SampleController :控制器{列表<博客>lstBlogs;公共示例控制器(){lstBlogs = 新列表<博客>{新博客{ Name="Domnic", URL="www.google.com"},新博客{ Name="Tom", URL="www.YAHOO.com"},新博客{ Name="Cat", URL="www.facebook.com"},新博客{ Name="Bob", URL="www.twitter.com"}};}//获取:样本公共 ActionResult 索引(){返回视图(lstBlogs);}公共 ActionResult IndexWithDynamicView(){返回视图(lstBlogs);}[HttpPost]公共无效创建(博客博客){}}公开课博客{公共字符串名称;公共字符串 URL;}}

解决方案

你的类 Blog 只包含字段,不包含属性,所以 DefaultModelBinder 不能设置他们的价值观.更改它添加 getter/setter

公开课博客{公共字符串名称 { 获取;放;}公共字符串 URL { 获取;放;}}

I am just trying to learn MVC and facing some issues.When I am submitting my partial view, I am getting null in Model Blog inside Create Method.

What I am doing wrong and what is the right approach?

View(Index.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())

Partial View(_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>

SampleController.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;
    }

}

解决方案

Your class Blog only contains fields, not properties so the DefaultModelBinder cannot set their values. Change it add getters/setters

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

这篇关于在MVC5中以局部视图提交表单时模型为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆