首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MVC 4--邮政零数据

MVC 4--邮政零数据
EN

Stack Overflow用户
提问于 2014-05-29 20:38:24
回答 2查看 47关注 0票数 0

我显然在这里错过了什么,我无法为我的一生想出什么。我可以很好地填充模型并将其发送到视图,但是在post中,大多数数据都是空的或默认的。

我强制使用/ test /createa1=5&a2=6&shell=7来输入Create,这对于初始化测试实体来说很好。POST具有名称和CatId,但其他属性为null。

任何帮助都将不胜感激。

模型

代码语言:javascript
复制
namespace MyApp.Models
{
    public partial class TestEntity
    {
        [DisplayName("Entity Id")]
        [Required]
        public int? EntityId { get; set; }

        [Required]
        [DisplayName("Entity Name")]
        public string EntityName { get; set; }

        [DisplayName("Category Id")]
        [Required]
        public int? CatId { get; set; }

        [DisplayName("Attribute 1")]
        public int? Attribute1 { get; set; }

        [DisplayName("Attribute 2")]
        public int? Attribute2 { get; set; }        
    }
}

视图

代码语言:javascript
复制
@model MyApp.Models.TestEntity

<h2>Test</h2>
@using (Html.BeginForm())
{

    @Html.AntiForgeryToken()
    @Html.ValidationSummary()
    <p>
        @Html.LabelFor(model => model.EntityName)
        @Html.TextBoxFor(model => model.EntityName)
    </p>
    <p>
        @Html.LabelFor(model => model.CatId)
        @Html.TextBoxFor(model => model.CatId)
    </p>

    <p>
        @Html.LabelFor(model => model.Attribute1)
        @Html.DisplayFor(model => model.Attribute1)
    </p>

    <p>
        @Html.LabelFor(model => model.Attribute2)
        @Html.DisplayFor(model => model.Attribute2)
    </p>
    <input type="submit" value="Done" />

}

控制器

代码语言:javascript
复制
using System.Web.Mvc;
using MyApp.Models;

namespace MyApp.Controllers
{
    public class TestController : Controller
    {
        //
        // GET: /Test/Create
        public ActionResult Create(int? a1, int? a2, int? shell)
        {
            using (var db = new MyDbContext())
            {
                ShellEntity temp =
                    db.ShellEntities.Where(se => se.ShellId == shell).FirstOrDefault();

            TestEntity model = new TestEntity();
            model.CatId = temp.category; //get the category id from the shell
            model.Attribute1 = a1;
            model.Attribute2 = a2;
            return View(model);
            }
        }

        //
        // POST: /Test/Create
        [HttpPost]
        public ActionResult Create(TestEntity model)
        {
            try
            {
                //at this point model.Attribute1 and Attribute2 are both null
                if (!model.Attribute1.HasValue)
                {
                    //WTF???
                }
                return View(model);
            }
            catch
            {
                return View(model);
            }
        }
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-05-29 20:58:41

你需要用

代码语言:javascript
复制
@Html.HiddenFor(model => model.Attribute1)

将值传递给控制器而不是DisplayFor()

票数 0
EN

Stack Overflow用户

发布于 2014-05-29 21:08:34

更改此代码

代码语言:javascript
复制
<p>
    @Html.LabelFor(model => model.Attribute1)
    @Html.DisplayFor(model => model.Attribute1)
</p>

<p>
    @Html.LabelFor(model => model.Attribute2)
    @Html.DisplayFor(model => model.Attribute2)
</p>

对此:

代码语言:javascript
复制
<p>
    @Html.LabelFor(model => model.Attribute1)
    @Html.HiddenFor(model => model.Attribute1)
    @Html.DisplayFor(model => model.Attribute1)
</p>

<p>
    @Html.LabelFor(model => model.Attribute2)
    @Html.HiddenFor(model => model.Attribute2)
    @Html.DisplayFor(model => model.Attribute2)
</p>

而且它应该能正常工作。

基本上,它添加了<input name="Attribute1" value="your_value"/>,所以在单击submit按钮后,它将被包含到POST中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23942538

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档