ASP.NET MVC Html.BeginForm通过ActionMethod插入新的数据库记录

时间:2018-05-22 13:04:59

标签: asp.net asp.net-mvc forms razor

当我按下提交“创建”按钮时,页面刷新并且没有任何反应,就像表单甚至没有调用控制器“SaveProject”actionresult一样。没有任何内容插入数据库。

我注意到点击提交按钮时浏览器中提供的网址为“http://localhost:62234/Operations/CreateProjectView?ProjectName=bhihi

不应该是SaveProject吗?为什么不将它指向正确的URL?当我输入以下网址时:“http://localhost:62234/Operations/SaveProject?ProjectName=bhihi

它创建了一个项目并且工作正常。但由于某种原因,表单没有调用正确的名称??

这是我的代码:

OperationsController:

   public ActionResult CreateProjectView()
    {
        return View();
    }

    public ActionResult SaveProject(Project model)
    {

        try
        {

            IRISInSiteLiveEntities DB = new IRISInSiteLiveEntities();

            Project newproject = new Project();

            newproject.ProjectName = model.ProjectName;


            DB.Projects.Add(newproject);
            DB.SaveChanges();


            return RedirectToAction("CreateProjectView");

        }
        catch(Exception ex)
        {
            throw ex;
        }

    }

CreateNewProject查看:

 @model IRIS.Models.Project

 @{
     ViewBag.Title = "CreateProjectView";
     Layout = "~/Views/Shared/_Layout.cshtml";
 }

<div class="page-wrapper nope" style="background-image: url(../assets/images/background/AdobeStock_56278.png); background-size:auto">
    <div class="container-fluid">
        <div class="row">
            <div class="col-lg-12">
                <div class="card">
                    <div class="card-body">
                        <div class="form-body">

                            @using (Html.BeginForm("SaveProject", "Operations", FormMethod.Post))
                            {

                                <div class="row">
                                    <div class="col-6">
                                        <div class="row">
                                            <div class="col-lg-4 col-sm-6 offset-lg-2">
                                                <label class="control-label">Project Name</label>
                                            </div>
                                            <div class="col-lg-4 col-sm-6">
                                                @Html.TextBoxFor(model => model.ProjectName, new { @class = "form-control", Style = "text-align:center" })
                                            </div>
                                        </div>
                                        <br />
                                        <div class="row">
                                            <div class="col-lg-4 col-sm-6 offset-lg-2">
                                                <label class="control-label">Network Operator</label>
                                            </div>
                                            <div class="col-lg-4 col-sm-6">

                                            </div>
                                        </div>
                                        <br />
                                        <div class="row">
                                            <div class="col-lg-4 col-sm-6 offset-lg-2">
                                                <label class="control-label">Nokia Customer</label>
                                            </div>
                                            <div class="col-lg-4 col-sm-6">

                                            </div>
                                        </div>
                                        <br />
                                        <div class="row">
                                            <div class="col-lg-4 col-sm-6 offset-lg-2">
                                                <label class="control-label">Nokia Project Business Manager</label>
                                            </div>
                                            <div class="col-lg-4 col-sm-6">

                                            </div>
                                        </div>
                                        <br />
                                        <div class="row">
                                            <div class="col-lg-4 col-sm-6 offset-lg-2">
                                                <label class="control-label">Nokia Cost and Progress Manager</label>
                                            </div>
                                            <div class="col-lg-4 col-sm-6">

                                            </div>
                                        </div>
                                        <br />
                                        <div class="row">
                                            <div class="col-lg-4 col-sm-6 offset-lg-2">
                                                <label class="control-label">Nokia Procurement Manager</label>
                                            </div>
                                            <div class="col-lg-4 col-sm-6">

                                            </div>
                                        </div>
                                    </div>

                                    <div class="col-6">
                                        <div class="row">
                                            <div class="col-lg-4 col-sm-6">
                                                <label class="control-label">Nokia PDM</label>
                                            </div>
                                            <div class="col-lg-4 col-sm-6">

                                            </div>
                                        </div>
                                        <br />
                                        <div class="row">
                                            <div class="col-lg-4 col-sm-6">
                                                <label class="control-label">Nokia Project Manager</label>
                                            </div>
                                            <div class="col-lg-4 col-sm-6">

                                            </div>
                                        </div>
                                        <br />
                                        <div class="row">
                                            <div class="col-lg-4 col-sm-6">
                                                <label class="control-label">Nokia Project Engineer</label>
                                            </div>
                                            <div class="col-lg-4 col-sm-6">

                                            </div>
                                        </div>
                                        <br />
                                        <div class="row">
                                            <div class="col-lg-4 col-sm-6">
                                                <label class="control-label">Iris Project No</label>
                                            </div>
                                            <div class="col-lg-4 col-sm-6">

                                            </div>
                                        </div>
                                        <br />
                                        <div class="row">
                                            <div class="col-lg-4 col-sm-6">
                                                <label class="control-label">Iris Project Manager</label>
                                            </div>
                                            <div class="col-lg-4 col-sm-6">

                                            </div>
                                        </div>
                                        <br />
                                        <div class="row">
                                            <div class="col-lg-4 col-sm-6">
                                                <label class="control-label">Iris Project Engineer</label>
                                            </div>
                                            <div class="col-lg-4 col-sm-6">

                                            </div>
                                        </div>
                                    </div>
                                </div>

                                <br /><br />

                                <div class="col-2 offset-lg-1 pull-left">
                                    <div class="input-group">
                                        <input type="submit" class="btn btn-block btn-outline-info"  value="Create" style="width:100%; height:50px; padding: 5px" />
                                    </div>
                                </div>

                            }
                        </div>
                        </div>
                </div>
            </div>
        </div>
    </div>
</div>

没有任何错误或任何错误。没有例外。感谢。

2 个答案:

答案 0 :(得分:0)

首先,你想在你的控制器中使用 Http decorators

[HttpGet]
public IActionResult CreateProjectView()
{
    return View();
}

[HttpPost]
public IActionResult CreateProject(Project model)
{
    try
    {
        // This should be your repository.
        var DB = new IRISInSiteLiveEntities();

        var newProject = new Project
        {
            ProjectName = model.ProjectName
        };

        DB.Projects.Add(newproject);
        DB.SaveChanges();

        return RedirectToAction("CreateProjectView");
    }
    catch(Exception ex)
    {
        throw ex;
    }
}

你认为应该是这样的(这只是你的一部分):

@model IRIS.Models.Project

<form asp-controller="Operations" asp-action="CreateProject" id="operationsForm" method="post">

            @Html.AntiForgeryToken()

            <div class="form-group">
                <label asp-for="ProjectName">Project Name</label>
                <input asp-for="ProjectName" class="form-control" />
            </div>

            <div class="form-group">
                <label asp-for="NetworkOperator">NetworkOperator</label>
            </div>

            ...

            <div class="form-group">
                <input id="submit" type="submit" value="Create" class="btn btn-default">
            </div>
</form>

现在,我不记得确切,但这是一种'更新'的剃刀语法(我不使用Razor)。它看起来更像HTML,它更清洁。你必须在互联网上查找。

你应该看的是存储库模式。我认为IRISInSiteLiveEntities是您的存储库。这应该实现像CreateProject()这样的方法,这样你就不用直接从你的Controller写 - 这不是Controller的工作。

所以,你的Controller应该是这样的:

private readonly IRISInSiteLiveEntities m_siteLiveEntities;

[HttpGet]
public IActionResult CreateProjectView()
{
    return View();
}

[HttpPost]
public IActionResult CreateProject(Project model)
{
    try
    {
        var newProject = new Project
        {
            ProjectName = model.ProjectName
        };

        m_siteLiveEntities.CreateProject(newProject);

        return RedirectToAction("CreateProjectView");
    }
    catch(Exception ex)
    {
        throw ex;
    }
}

再次,寻找 MVC架构设计存储库模式,甚至清洁架构(洋葱架构)

答案 1 :(得分:0)

当我将Layout =更改为NULL时,表单有效。当我查看_Layout.cshtml时,页面被包含在标签中。删除后,表单即可生效。愚蠢的我。