使用Http Post在Razor中使用@model时的困惑

时间:2014-10-30 16:15:07

标签: c# asp.net-mvc entity-framework razor

我有一个应用程序,主要是使用ET 6 DB First + controller / views scaffolding在此时生成的。我对Razor和我的控制器的帖子感到困惑。基本上我的数据没有超过DOM

这是我的控制器:(不介意验证设置,这不是面向公众的应用程序)

namespace RegTeg.Controllers
{
    public class HomeController : Controller
    {
        private VRIIntegrationEntities db = new VRIIntegrationEntities();
        public ActionResult Index()
        {

            return View(db.ApplicationNames.ToList());
        }


        [HttpPost]
        // [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        public ActionResult Index([Bind(Include = "appName")] RegTeg.Models.ApplicationName newAppName)
        {
            if (ModelState.IsValid)
            {
                db.ApplicationNames.Add(newAppName);
                db.SaveChanges();   //fails here due to a null column
                return RedirectToAction("Index");
            }

            return View(newAppName);
        }

    }
}

所以我有我的索引,我在其中向视图提供了一个列表,这样我就可以在访问页面时将数据从我的数据库显示给用户。在这个页面上还有一个帖子,他们可以在数据库中添加一些内容。我传递了我的模型实例,然后插入表中。所以这一切都很好(所以我认为)。

现在我的观点:

@model IEnumerable<RegTeg.Models.ApplicationName>

<div>
    <select class="applicationSelect">
        @foreach (var item in Model)
        {
            <option>
                @Html.DisplayFor(modelItem => item.appName)
            </option>
        }
    </select>
</div>

@using (Html.BeginForm())
{
    RegTeg.Models.ApplicationName appModel = new RegTeg.Models.ApplicationName();
    <div class="form-horizontal">
        @Html.ValidationSummary(true)
        @Html.AntiForgeryToken()

        <h3>Add New Application for Testing:</h3>
        <div class="form-group">
            @Html.LabelFor(model => appModel.appName, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => appModel.appName)
                @Html.ValidationMessageFor(model => appModel.appName)
            </div>
        </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>
}

我传入了我的强类型模型。在我的foreach循环中获取数据...好吧,已经完成了,现在我想在我的服务器上发帖并向我的数据库添加新数据。我创建了一个新的模型实例,从我的输入和繁荣中提取数据,提交它。但问题是,数据是null。我有预感,它与我的@model声明有关。例如,帖子需要@model RegTeg.Models.ApplicationName,但是我认为在我的视图中稍后声明新实例时没有必要?我希望我能很好地表达自己。提前谢谢。

2 个答案:

答案 0 :(得分:1)

你的问题是你在视图中使用了两个模型:

1)模型IEnumerable是列表的模型。 2)模型RegTeg.Models.ApplicationName是一个元素的模型。

存在不同的修复方法,我喜欢在PartialView中制作表单,

有关在视图http://www.codeproject.com/Articles/687061/Multiple-Models-in-a-View-in-ASP-NET-MVC-MVC

中使用多个模型的其他信息和其他解决方案

答案 1 :(得分:1)

问题1:

我猜你还想要显示你的所有应用程序,所以我建议你只是在你最初做的之后对你的帖子进行建模:

[HttpPost]
// [ValidateAntiForgeryToken]
[ValidateInput(false)]
public ActionResult Index([Bind(Include = "appName")] RegTeg.Models.ApplicationName newAppName)
{
    if (ModelState.IsValid)
    {
        db.ApplicationNames.Add(newAppName);
        db.SaveChanges();   //fails here due to a null column
        return RedirectToAction("Index");
    }
    //continue to return a list as your view is expecting
    //if you REALLY want only the item that you just created, then construct a list, 
    //add that to it, and return that instead of the full list
    return View(db.ApplicationNames.ToList());
}

问题2:

查看您的HTML。您需要做的是确保文本框中的NAME属性与ActionMethod的变量名称相同。例如,如果您的文本框是这样的:

<input type="text" name="appName" id="appName" />

然后我会建议摆脱你所拥有的绑定东西,然后这样做:

[HttpPost]
// [ValidateAntiForgeryToken]
[ValidateInput(false)]
public ActionResult Index(RegTeg.Models.ApplicationName appName)
{
    ... existing code here ...
}