ASP.NET MVC:发布到控制器时表单为空

问题描述 投票:0回答:1

我有一个非常简单的页面,其中有一个非常简单的模型,我不完全确定为什么当我发布模型时它是空的。我正在寻求一些帮助,我现在已经盯着这段代码两个小时了,我一定是忽略了一些东西。另外,当

Model.State
无效时,网址将更改为没有我的 ID。

这是控制器:

using Stonks.Model;
using Stonks.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace StockCenter.Controllers
{
    public class TradeIdeaController : Controller
    {
        // GET: TradeIdea
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Create(int Id)
        {
            //var dailyOptionRecord = new OptionHeaderService().GetById(Id);
            var model = new Models.CreateTradeIdeaModel();
            model.Ticker = "something";
            return View(model);
        }

        [HttpPost]
        public ActionResult Create(Models.CreateTradeIdeaModel model)
        {
            if (ModelState.IsValid)
            {
                var newIdea = new TradeIdea()
                {
                    CreatedBy = "StockCenter",
                    CreatedDate = DateTime.UtcNow,
                    LastUpdateBy = "StockCenter",
                    LastUpdateDate = DateTime.UtcNow,
                    DiscoveryDate = DateTime.UtcNow,
                    ExpirationDate = DateTime.UtcNow.AddDays(14),
                    Ticker = model.Ticker,
                    TradeDescription = model.TradeDescription,
                    TradeDirection = model.TradeDirection
                };

                new TradeIdeaService().Insert(newIdea);

                //return View(model);
                return Content(@"<body>
                       <script type='text/javascript'>
                         window.close();
                       </script>
                     </body> ");
            }

            return View(model);
        }
    }
}

这是模型类:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace StockCenter.Models
{
    public class CreateTradeIdeaModel
    {
        [Required]
        public string Ticker { get; set; }
        [Required]
        public string TradeDirection { get; set; }
        [Required]
        public string TradeDescription { get; set; }
    }
}

这是页面:

@using Kendo.Mvc.UI
@model StockCenter.Models.CreateTradeIdeaModel

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

@using (Html.BeginForm("Create", "TradeIdea", FormMethod.Post))
{
    // Form Elements here\
    <table align="center">
        <tr>
            <td align="center">
                @Html.Label("Ticker:")
                @Html.TextBox("Ticker_Box",Model.Ticker)
            </td>
        </tr>
        <tr>
            <td align="center">
                @Html.Label("TradeDirection:")
                @Html.TextBox("Trade_Direction")
            </td>
        </tr>
        <tr>
            <td align="center">
                @Html.Label("TradeDescription:")
                @Html.TextBox("Trade_Description")
            </td>
        </tr>
        <tr>
            <td align="center">
                <input type="submit" value="Submit Idea" id="btnSubmit" />
            </td>
        </tr>
    </table>
}
c# html asp.net-mvc
1个回答
0
投票

正如评论中提到的,您为渲染的

name
元素自定义了
<input>
属性。这些
name
属性与您的
CreateTradeIdeaModel
类中的属性不同。

确保属性名称与您提供的

name
匹配。

@Html.TextBox("TickerBox",Model.Ticker)

@Html.TextBox("TradeDirection")

@Html.TextBox("TradeDescription")

或者您可以使用

Html.TextBoxFor()
Html.EditorFor()
,编译器会建议您从模型中选择属性。这适用于模型绑定。

@Html.TextBoxFor(m => m.Ticker)

@Html.TextBoxFor(m => m.TradeDirection)

@Html.TextBoxFor(m => m.TradeDescription)
© www.soinside.com 2019 - 2024. All rights reserved.