Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I tell what's happening when I postback my view?
    primarykey
    data
    text
    <p>I'm working with MVC 4 EF Code First and I have an object with an Edit View and it works properly. Then I created a very similar Create View, only when I click on the submit button I don't hit any breakpoints in the Controller. How do I tell what's wrong here? Is there somewhere I can set a breakpoint in either JavaScript or code-behind?</p> <p><strong>Model</strong></p> <pre><code>public class ThirdParty : BaseModel { public Int32 Id { get; set; } [MaxLength(2)] private String CountryCode { get; set; } [MaxLength(6)] private Int32 SequenceNumber { get; set; } public String UIN { get { if (CountryCode != null &amp;&amp; SequenceNumber &gt; 0) return CountryCode.ToString() + "-" + SequenceNumber.ToString(); else return null; } set { CountryCode = value.ToString().Substring(0, 2); SequenceNumber = Int32.Parse(value.ToString().Substring(3, value.Length-3)); } } [MaxLength(250)] [Required(AllowEmptyStrings = false, ErrorMessage = "{0} is required.")] public String Name { get; set; } public virtual ICollection&lt;RelationshipType&gt; RelationshipTypes { get; set; } // Address public Int32 AddressId { get; set; } [ForeignKey("AddressId")] public virtual Address Address { get; set; } public bool IsImported { get; set; } public bool IsActive { get; set; } public virtual ICollection&lt;Attachment&gt; Attachments { get; set; } // constructor public ThirdParty() { RelationshipTypes = new HashSet&lt;RelationshipType&gt;(); Attachments = new HashSet&lt;Attachment&gt;(); } } </code></pre> <p><strong>Controller Methods</strong></p> <pre><code>public ActionResult Edit(int id) { ThirdPartyContext context = new ThirdPartyContext(); ThirdParty model = context.ThirdParties.Find(id); ViewBag.Countries = context.Countries.ToList&lt;Country&gt;(); return View(model); } [HttpPost] public ActionResult Edit(string button, ThirdParty model, int id) { if (button == "cancel") return RedirectToAction("Index"); if (ModelState.IsValid) { ThirdPartyContext context = new ThirdPartyContext(); model.Address.Country = context.Countries.Find(model.Address.CountryId); context.Entry(model.Address.Country).State = EntityState.Modified; context.Entry(model.Address).State = EntityState.Modified; context.Entry(model).State = EntityState.Modified; context.SaveChanges(); Success("Third Party record updated!"); return RedirectToAction("Index"); } else { ThirdPartyContext context = new ThirdPartyContext(); model.Address = context.Addresses.Find(model.AddressId); return View("Edit", model); } } public ActionResult Create() { ThirdPartyContext context = new ThirdPartyContext(); ViewBag.Countries = context.Countries.ToList&lt;Country&gt;(); return View(); } [HttpPost] public ActionResult Create(ThirdParty model) { if (ModelState.IsValid) { ThirdPartyContext context = new ThirdPartyContext(); List&lt;ThirdParty&gt; models = context.ThirdParties.ToList&lt;ThirdParty&gt;(); model.Id = models.Count() == 0 ? 1 : models.Select(x =&gt; x.Id).Max() + 1; context.ThirdParties.Add(model); context.SaveChanges(); Success("Your information was saved!"); return RedirectToAction("Index"); } Error("There were some errors in your form."); return View(model); } [HttpPost] public ActionResult Create(string button, ThirdParty model) { if (button == "cancel") return RedirectToAction("Index"); if (ModelState.IsValid) { ThirdPartyContext context = new ThirdPartyContext(); List&lt;ThirdParty&gt; models = context.ThirdParties.ToList&lt;ThirdParty&gt;(); model.Id = models.Count() == 0 ? 1 : models.Select(x =&gt; x.Id).Max() + 1; context.ThirdParties.Add(model); context.SaveChanges(); Success("Your information was saved!"); return RedirectToAction("Index"); } Error("There were some errors in your form."); return View(model); } </code></pre> <p><strong>Edit View</strong></p> <pre><code>@model Models.ThirdParty @{ ViewBag.Title = "Edit"; Layout = "~/Views/shared/ContentLayout.cshtml"; } &lt;div class="row"&gt; &lt;div class="col-lg-12"&gt; &lt;div class="page-header"&gt; &lt;h2&gt;Third Party&lt;/h2&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;div class="row"&gt; &lt;div class="col-lg-8"&gt; @using (Html.BeginForm("Edit", "ThirdParty", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal" })) { @Html.AntiForgeryToken() @Html.ValidationSummary(true) &lt;div class="form-group"&gt; @Html.HiddenFor(model =&gt; model.Id) &lt;div class="clearfix visible-xs"&gt;&lt;/div&gt; @Html.HiddenFor(model =&gt; model.Address.Id) &lt;/div&gt; &lt;div class="form-group"&gt; @Html.EditorFor(model =&gt; model.UIN) &lt;div class="clearfix visible-xs"&gt;&lt;/div&gt; @Html.EditorFor(model =&gt; model.Name) &lt;/div&gt; @Html.HiddenFor(model =&gt; model.AddressId) @Html.Partial("_EditAddress", Model) @Html.HiddenFor(model =&gt; model.CreatedBy) @Html.HiddenFor(model =&gt; model.CreatedOn) @Html.HiddenFor(model =&gt; model.ModifiedBy) @Html.HiddenFor(model =&gt; model.ModifiedOn) &lt;p&gt; &lt;button name="button" type="submit" class="btn btn-default" value="cancel"&gt;Cancel&lt;/button&gt; &lt;button name="button" type="submit" class="btn btn-primary" value="submit"&gt;Submit&lt;/button&gt; &lt;/p&gt; } &lt;/div&gt; &lt;/div&gt; </code></pre> <p>** Create View **</p> <pre><code>@model Models.ThirdParty @{ ViewBag.Title = "Create"; Layout = "~/Views/shared/ContentLayout.cshtml"; } &lt;div class="row"&gt; &lt;div class="col-lg-12"&gt; &lt;ul class="breadcrumb" style="margin-bottom: 5px;"&gt; &lt;li&gt;&lt;a href="#"&gt;Third Parties&lt;/a&gt;&lt;/li&gt; &lt;li class="active"&gt;Create New Third Party&lt;/li&gt; &lt;/ul&gt; &lt;/div&gt; &lt;/div&gt; &lt;div class="row"&gt; &lt;div class="col-lg-12"&gt; &lt;div class="page-header"&gt; &lt;h2&gt;Create Third Party&lt;/h2&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;div class="row"&gt; &lt;div class="col-lg-2"&gt;&amp;nbsp;&lt;/div&gt; &lt;div class="col-lg-8"&gt; @using (Html.BeginForm("Create", "ThirdParty", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal" })) { @Html.AntiForgeryToken() @Html.ValidationSummary(true) @Html.EditorFor(model =&gt; model.UIN) @Html.EditorFor(model =&gt; model.Name) @Html.HiddenFor(model =&gt; model.AddressId) @Html.Partial("_EditAddress", Model) @Html.HiddenFor(model =&gt; model.CreatedBy) @Html.HiddenFor(model =&gt; model.CreatedOn) @Html.HiddenFor(model =&gt; model.ModifiedBy) @Html.HiddenFor(model =&gt; model.ModifiedOn) &lt;p&gt; &lt;button name="button" type="submit" class="btn btn-default" value="cancel"&gt;Cancel&lt;/button&gt; &lt;input name="button" type="submit" class="btn btn-primary" value="submit" title="Submit" /&gt; &lt;input type="submit" value="Create" /&gt; &lt;/p&gt; } &lt;/div&gt; &lt;div class="col-lg-2"&gt;&amp;nbsp;&lt;/div&gt; &lt;/div&gt; </code></pre> <p><strong>Edit:</strong></p> <p>I may have left out a critical part. I have these 4 hidden fields on the ThirdParty and Address models that I am planning to set in the controller Create method (if I can ever get in there). Here's the Model:</p> <pre><code>public class BaseModel { [HiddenInput(DisplayValue = false)] public Int32 CreatedBy { get; set; } [HiddenInput(DisplayValue = false)] [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)] public DateTime CreatedOn { get; set; } [HiddenInput(DisplayValue = false)] public Int32 ModifiedBy { get; set; } [HiddenInput(DisplayValue = false)] [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)] public DateTime ModifiedOn { get; set; } } </code></pre> <p>I was thinking that maybe my view is invalid and that's why it's not sending it to the controller. Here's how these look in the page markup:</p> <pre><code> &lt;input data-val="true" data-val-number="The field CreatedBy must be a number." data-val-required="The CreatedBy field is required." id="Address_CreatedBy" name="Address.CreatedBy" type="hidden" value="" /&gt; &lt;input data-val="true" data-val-date="The field CreatedOn must be a date." data-val-required="The CreatedOn field is required." id="Address_CreatedOn" name="Address.CreatedOn" type="hidden" value="" /&gt; &lt;input data-val="true" data-val-number="The field ModifiedBy must be a number." data-val-required="The ModifiedBy field is required." id="Address_ModifiedBy" name="Address.ModifiedBy" type="hidden" value="" /&gt; &lt;input data-val="true" data-val-date="The field ModifiedOn must be a date." data-val-required="The ModifiedOn field is required." id="Address_ModifiedOn" name="Address.ModifiedOn" type="hidden" value="" /&gt; &lt;div class="form-group"&gt; &lt;input data-val="true" data-val-number="The field CreatedBy must be a number." data-val-required="The CreatedBy field is required." id="CreatedBy" name="CreatedBy" type="hidden" value="" /&gt; &lt;input data-val="true" data-val-date="The field CreatedOn must be a date." data-val-required="The CreatedOn field is required." id="CreatedOn" name="CreatedOn" type="hidden" value="" /&gt; &lt;input data-val="true" data-val-number="The field ModifiedBy must be a number." data-val-required="The ModifiedBy field is required." id="ModifiedBy" name="ModifiedBy" type="hidden" value="" /&gt; &lt;input data-val="true" data-val-date="The field ModifiedOn must be a date." data-val-required="The ModifiedOn field is required." id="ModifiedOn" name="ModifiedOn" type="hidden" value="" /&gt; &lt;/div&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload