我所有的文本框都返回null我应该怎么办? [英] all my textboxes return null what should I do?

查看:96
本文介绍了我所有的文本框都返回null我应该怎么办?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即使我在文本框中输入了某些内容,我在返回null的页面上的元素上也遇到了问题.是什么原因造成的?我想用最后一个年度的仪表板制作一个简单的CRUD应用.

I have a problem here with regards to elements on my pages returning null even though I have typed something in the textbox. What causes this? I want to make a simple CRUD app with a dashboard for final year.

这是我的观点:

@model WebApplication1.Models.Category

@{
    ViewBag.Title = "Create Category";
}

<h2>@ViewBag.Title</h2>

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.Name, htmlAttributes: new { @class 
        ="control-label col-md-2" })
        <div class="col-md-10">
            @Html.TextBoxFor(model => model.Name, new { htmlAttributes = 
            new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Name, "", new { 
            @class = "text-danger" })
        </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>
} 

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

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

这是我的控制器动作:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "ID,Name")] Category category)
{
    if (ModelState.IsValid)
    {
        db.Categories.Add(category);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    return View(category);
}

推荐答案

我认为您需要发布到正确的ActionName.您使用@using (Html.BeginForm()),它将发布到控制器的索引.但是您有Create.因此,将表格指向该页面.

I think you need to post to the correct ActionName. You use @using (Html.BeginForm()), which will post to the Index of a Controller. But you have Create. So point the form to that.

@using (Html.BeginForm("Create", "Home", FormMethod.Post))

这篇关于我所有的文本框都返回null我应该怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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