Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • AWatters 33 posts 174 karma points
    May 24, 2019 @ 20:30
    AWatters
    0

    I have the following form which is not working as planned. Can't use Umbraco Forms for it

    Model:

    public AssessmentTypePageViewModel() : base(UmbracoContext.Current.PublishedContentRequest.PublishedContent)
        {
            CompanyName = "Amazon";
            PostCode = "BT15 3TH";
        } 
    
        public string CompanyName { get; set; }
        [Required]
         public string PostCode { get; set; }
    

    View:

    @inherits UmbracoViewPage<TheWebsite.Models.Views.AssessmentTypePageViewModel>
    @using Umbraco.Forms.Mvc.Models;
    
    <div class="form-group">
          @Html.LabelFor(x => Model.CompanyName, new { @class = "form-control" })
           @Html.TextBoxFor(x => Model.CompanyName, new { autocapitalize = "off", @class = "form-control first last", autofocus = "", placeholder = "Company Name", type = "text" })
    </div>                           
            <div class="form-group">                               
                 <label for="Model.PostCode" class="control-label"> PostCode <em class="req">*</em>                             </label>
               @Html.TextBoxFor(x => Model.PostCode, new { @class = "form-control" })
               @Html.ValidationMessageFor(x => Model.PostCode, "Please fill in postcode")
    </div> 
    

    which generates

    <input autocapitalize="off" autofocus="" class="form-control first last valid" id="CompanyName" name="CompanyName" placeholder="Company Name" type="text" value="Amazon">           
    

    Controller:

     [HttpPost]
            public ActionResult Index(AssessmentTypePageViewModel Model)
            {
                    UmbracoContext.Current.HttpContext.Trace.Warn("companyname", Model.CompanyName);
                    UmbracoContext.Current.HttpContext.Trace.Warn("PostCode", Convert.ToString(Model.PostCode));
    

    I hope I have given enough information here.

    Anyway, the Binding works in that the Company Name field has "Amazon" in it when it is shown, but once this is changed and submitted, the Trace still shows "Amazon" afterwards, even though the value of CompanyName should have been changed.

    I'm a bit new to all this MVC stuff so hopefully it's just something obvious that I'm missing. Thanks

  • AWatters 33 posts 174 karma points
    May 27, 2019 @ 14:35
    AWatters
    100

    Replying to myself:

    As per: http://ideasof.andersaberg.com/development/aspnet-mvc-4-model-binding-null-on-post

    I changed the Controller method to

    public ActionResult Index(AssessmentTypePageViewModel incomingModel)

    and it now works.

Please Sign in or register to post replies

Write your reply to:

Draft