Search code examples
c#htmlasp.net-mvc

ASP.NET MVC : form is null when posting to controller


I have a very simple page with a very simple model and I'm not entirely sure why when I post the model it's null. I'm looking for a bit of assistance, I've now been staring at this code for 2 hours and I must be over looking something. Also, when the Model.State is not valid, the url changes to not having my Id.

Here is the controller:

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);
        }
    }
}

This is the model class:

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; }
    }
}

And here is the page:

@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>
}

Solution

  • As mentioned in the comment, you customized the name attribute for the <input> element rendered. And those name attributes are not the same as the properties in your CreateTradeIdeaModel class.

    Make sure that the property names are matched with your provided names.

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

    Or you can work with Html.TextBoxFor() or Html.EditorFor() which the compiler will suggest you choose the property from the model. This works with model binding.

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