Values in model are null or empty - asp.net-mvc

I have this action method
[HttpPost]
public ActionResult CreateEsf(EsfLotDetailsModel model)
{
...
}
I have two properties in this model. One is a database POCO object and the other is a list. In the GET equivalent of this method, these values were all populated correctly, but on post these get set to null (the POCO) and empty (the list).
Why might this be?
My view is here
#using UI.Helpers
#model UI.Areas.Admin.Models.EsfLotDetailsModel
#{
ViewBag.Title = "Create Forward Lot";
}
<script src="#Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="#Url.Content("~/Content/AdminDesignTheme/js/wl_Date.js")" type="text/javascript"></script>
<script src="#Url.Content("~/Content/AdminDesignTheme/js/wl_Time.js")" type="text/javascript"></script>
<script src="#Url.Content("~/Scripts/jquery.mousewheel.min.js")" type="text/javascript"></script>
#{
<script type="text/javascript">
$(document).ready(function () {
var options = {
dateFormat: "dd/mm/yy"
};
$('#Starts').wl_Date(options);
$('#Ends').wl_Date(options);
$('#startTime').wl_Time();
$('#endTime').wl_Time();
});
</script>
}
<p>#Html.ActionLink("Back to Item List", "Index","Inventory")</p>
#Html.ValidationSummary(true, "Please correct the errors and try again.")
#using (Html.BeginForm(new {model = #Model})) {
#Html.HiddenFor(model => model.Auction.InventoryReference)
#Html.HiddenFor(model => model.Auction.Title)
#Html.HiddenFor(model => model.Auction.Description)
<fieldset>
<legend></legend>
<label>ESF Lot Information</label>
<section>
#Html.LabelFor(model => model.Auction.Title)
<div> #Html.DisplayFor(model => model.Auction.Title)
#Html.ValidationMessageFor(model => model.Auction.Title) </div>
</section>
<section>
#Html.LabelFor(model => model.Auction.Description)
<div> #Html.DisplayFor(model => model.Auction.Description)
#Html.ValidationMessageFor(model => model.Auction.Description) </div>
</section>
#if (HttpContextHelper.IsUserAdmin())
{<section>
<label for="IsFeatured">Is Featured <i>(displayed in homepage)</i></label>
<div> #Html.CheckBoxFor(model => model.Auction.IsFeatured)
#Html.ValidationMessageFor(model => model.Auction.IsFeatured) </div>
</section>
}
else
{
<section>
<label for="IsFeatured">Is Featured <i>(displayed in homepage)</i></label>
<div> #Html.CheckBoxFor(model => model.Auction.IsFeatured, new { disabled = "disabled" }) (Contact Admin to make this a featured lot)
#Html.ValidationMessageFor(model => model.Auction.IsFeatured) </div>
</section>
}
#if (HttpContextHelper.IsUserAdmin())
{<section>
#Html.Label("VAT Applicable")
<div> #Html.CheckBoxFor(model => model.Auction.VatApplicable)
#Html.ValidationMessageFor(model => model.Auction.VatApplicable) </div>
</section>
}
else
{
<section>
#Html.Label("VAT Applicable")
<div> #Html.CheckBoxFor(model => model.Auction.VatApplicable, new { disabled = "disabled" }) (Contact Admin if it is not VATable)
#Html.ValidationMessageFor(model => model.Auction.VatApplicable) </div>
</section>
}
</fieldset>
<fieldset>
<legend></legend>
<label>Date and Time</label>
<section>
<label>Starts <em>(dd/mm/yy hh:mm)</em></label>
<div> <input type="text" class="date" id="Starts" />
<input type="text" class="time" data-connect="Starts" id="startTime" />
#Html.ValidationMessageFor(model => model.Auction.Starts) </div>
</section>
<section>
<label>Ends <em>(dd/mm/yy hh:mm)</em></label>
<div> <input type="text" class="date" id="Ends" />
<input type="text" class="time" data-connect="Ends" id="endTime" />
#Html.ValidationMessageFor(model => model.Auction.Ends) </div>
</section>
<section>
#Html.LabelFor(model => model.Auction.IsExtensible)
<div> #Html.CheckBoxFor(model => model.Auction.IsExtensible)
#Html.ValidationMessageFor(model => model.Auction.IsExtensible) </div>
</section>
</fieldset>
<fieldset>
<legend></legend>
<label>Bid Options</label>
<section>
#Html.LabelFor(model => model.Auction.StartingBid)
<div> #Html.TextBoxFor(model => model.Auction.StartingBid)
#Html.ValidationMessageFor(model => model.Auction.StartingBid) </div>
</section>
<section>
#Html.LabelFor(model => model.Auction.Reserve)
<div> #Html.TextBoxFor(model => model.Auction.Reserve)
#Html.ValidationMessageFor(model => model.Auction.Reserve) </div>
</section>
<section>
<label>Reserve Visible <em>(Displays as Reserve met or not met)</em></label>
<div> #Html.CheckBoxFor(model => model.Auction.ReserveVisible)
#Html.ValidationMessageFor(model => model.Auction.ReserveVisible) </div>
</section>
<section>
#Html.LabelFor(model => model.Auction.IsBidIncrementPercentual)
<div> #Html.CheckBoxFor(model => model.Auction.IsBidIncrementPercentual)
#Html.ValidationMessageFor(model => model.Auction.IsBidIncrementPercentual) </div>
</section>
<section>
#Html.LabelFor(model => model.Auction.BidIncrement)
<div> #Html.TextBoxFor(model => model.Auction.BidIncrement, new { #Value = 1m })
#Html.ValidationMessageFor(model => model.Auction.BidIncrement) </div>
</section>
<section>
#Html.LabelFor(model => model.AuctionEvents)
<div> #Html.DropDownList("Auction", Model.AuctionEvents, "Select auction", new { required = "required" })
#Html.ValidationMessageFor(model => model.AuctionEvents) </div>
</section>
<section>
<div><button>Create</button></div>
</section>
</fieldset>
}
<div>
#Html.ActionLink("Back to Item List", "Index","Inventory")
</div>

Add FormMethod = FormMethod.Post to your Html.BeginForm()
#using (Html.BeginForm("Action", "Controller",FormMethod.Post))
{
}

where is your submit button?
#using (Html.BeginForm("Action", "Controller",FormMethod.Post))
{
...
<input type="submit" value="submit" />
}
when you fill in the form, press the submit button, then enter in the [HttpPost] Method and value of Model will be filled.

None of these options worked. I put all the values in the POCO object separately in the model instead and removed the POCO object. No idea why it doesn't work.

Related

asp.net mvc 4 - form submit button not responding

I am new to MVC and is using MVc4 with VS2013. This is my controller:
[HttpPost]
public ActionResult Create(CreateRequestViewModel viewModel)
{
if (ModelState.IsValid)
{
return RedirectToAction("Index");
}
return View(viewModel);
}
Below is my view:
#model ProMs.Web.ViewModels.CreateRequestViewModel
#{
ViewBag.Title = "Create";
}
<body>
<h2>New Request</h2>
<h3></h3>
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
#Html.ValidationSummary(true)
<fieldset>
<div class="float-left">
<label for="RequestName">Request</label>
#Html.EditorFor(model => model.Request.RequestName)
#Html.ValidationMessageFor(model => model.Request.RequestName)
#Html.LabelFor(model => model.Request.Requestor)
#Html.EditorFor(model => model.Request.Requestor)
#Html.ValidationMessageFor(model => model.Request.Requestor)
#Html.LabelFor(model => model.Request.Purpose)
#Html.EditorFor(model => model.Request.Purpose)
#Html.ValidationMessageFor(model => model.Request.Purpose)
</div>
<div class="float-right">
#Html.LabelFor(model => model.Request.Investigator)
#Html.EditorFor(model => model.Request.Investigator)
#Html.ValidationMessageFor(model => model.Request.Investigator)
#Html.LabelFor(model => model.Request.Department)
#Html.EditorFor(model => model.Request.Department)
#Html.ValidationMessageFor(model => model.Request.Stage)
#Html.LabelFor(model => model.Request.Comment)
#Html.EditorFor(model => model.Request.Comment)
#Html.ValidationMessageFor(model => model.Request.Comment)
</div>
#Html.HiddenFor(model => model.Request.RequestID)
#Html.HiddenFor(model => model.Request.DateCreated)
#Html.HiddenFor(model => model.Request.CreatedBy)
</fieldset>
}
<p>
<input type="submit" value="Submit" />
</p>
</body>
Nothing happened if "submit" button is clicked. I even cold not put a breaking point at the line
.
Thanks very much for your help.
Hugh
#Html.BeginForm() creates a <form> element. Right now your submit button is outside of this element, so move it inside.
}
<p>
<input type="submit" value="Submit" />
</p>
Should be
<p>
<input type="submit" value="Submit" />
</p>
} <-- This goes down here

What is the VB.NET equivalence of this razor syntax?

I'm really having issues with the lambda expressions.
#model CalculateSimpleInterest.Models.SimpleInterestModel
#{
ViewBag.Title = "SimpleInterest";
}
<h2>Calulate Simple Interest</h2>#using (Ajax.BeginForm("CalculateSimpleInterestResult","CalculateSimpleInterest",
new AjaxOptions { UpdateTargetId = "divInterestDeatils" }))
{
<fieldset>
<legend>Calulate Simple Interest</legend><div id="div1"></div>
<div class="editor-label">
#Html.LabelFor(model => model.Amount)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Amount)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Rate)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Rate)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Year)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Year)
</div>
<p>
<input type="submit" value="Calculate" />
</p>
</fieldset>
}
#section Scripts {
#Scripts.Render("~/bundles/jqueryval")
}
The syntax you are looking for is
#Html.YourHelperFor(Function(model), model.Property)
eg
<div class="editor-label">
#Html.LabelFor(Function(model), model.Amount)
</div>

MVC form multipart/form-data isnt rendering correctly

here is my view. I am attempting to have a file upload control.
#section termimalContent {
#using (Html.BeginForm("Add", "Terminals_Policies", "multipart/form-data"))
{
#Html.ValidationSummary(true)
<fieldset>
<legend>
<h2>
Create new policy</h2>
</legend>
<p>
<strong>Assigning devices to Node:</strong> #Model.GroupName</p>
<div class="editor-label">
#Html.LabelFor(model => model.PolicyName)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.PolicyName)
#Html.ValidationMessageFor(model => model.PolicyName)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.PolicyType)
</div>
<div class="editor-field">
#Html.DropDownListFor(model => model.SelectedItems, new SelectList(Model.PolicyType, "Value", "Text"))
#Html.ValidationMessageFor(model => model.SelectedItems)
</div>
<div class="editor-label">
<label for="file1">
Filename:</label>
</div>
<div class="editor-field">
<input type="file" name="files" id="file1" />
</div>
<div class="editor-label">
<label for="file2">
Filename:</label>
</div>
<div class="editor-field">
<input type="file" name="files" id="file2" />
</div>
#Html.HiddenFor(model => model.GroupId)
#Html.HiddenFor(model => model.GroupName)
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
}
but the html is being rendered as:
<form action="/Terminals_Policies/Add/1?Length=19" id="form0" method="post">
Anyone know what i have done wrong
Try:
#using (Html.BeginForm("Add", "Terminals_Policies", FormMethod.Post,new{enctype="multipart/form-data"}))
As it should be in the format
Html.BeginForm(action, controller, FormMethod.Post, new { enctype="multipart/form-data"})

I don't know how to useform action on view razor

I am trying to save values in salesforce on my form submit button .
I dont know how to use form action method in view razor with validation ....
Here is my code for salesforce which I need to implement on submit button of my form so that it make an entry in salesforce:
<form action="https://www.salesforce.com/servlet/..." method="POST" >
<input type=hidden name="oid" value="111">
<input type=hidden name="retURL" value = "http://test" />
here is my view page:
#model nd.Models.PopupDemoModel
<script src="#Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="#Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
#using (Html.BeginForm()){
#Html.ValidationSummary(true)
<fieldset>
<div class="editor-label grid_2">
#Html.LabelFor(model => model.first_name )
</div>
<div class="editor-field grid_3">
#Html.TextBoxFor(model => model.first_name , new { #class = "demo-field-longer" })
</div>
<div class="grid_3 error long" style="margin-left:250px">
#Html.ValidationMessageFor(model => model.first_name )
</div>
<div class="clear"></div>
<div class="editor-label grid_2">
#Html.LabelFor(model => model.email )
</div>
<div class="editor-field grid_3">
#Html.TextBoxFor(model => model.email , new { #class = "demo-field-longer" })
</div>
<div class="grid_3 error long" style="margin-left:250px">
#Html.ValidationMessageFor(model => model.email )
</div>
<div class="clear"></div>
<div class="editor-label grid_2">
#Html.LabelFor(model => model.phone )
</div>
<div class="editor-field grid_3">
#Html.TextBoxFor(model => model.phone , new {#class="demo-field-longer"})
</div>
<div class="grid_3 error long" style="margin-left:250px">
#Html.ValidationMessageFor(model => model.phone )
</div>
<div class="clear"></div>
<div class="editor-label grid_2">
#Html.LabelFor(model => model.company )
</div>
<div class="editor-field grid_3">
#Html.TextBoxFor(model => model.company , new { #class = "demo-field-longer" })
</div>
<div class="grid_3 error long" style="margin-left:250px">
#Html.ValidationMessageFor(model => model.company)
</div>
<div class="clear"></div>
<div class="clear"></div>
<div class="grid_2 sub-spacer"> </div>
<div class="editor-field grid_2 submit">
<input type="submit" value="Submit" id="demo-submit-button"/><br />
#ViewData["DemoMessage"]
</div>
</fieldset>
}
Any help is appreciated....
You can do this as below:
#using (Html.BeginForm("Action", "Controller", FormMethod.Post, new { id = "cssClassorId" })) {
}

Ajax.BeginForm redirects action page(MVC 4)

I use 'Ajax.BeginForm' in my application,but for some reason my form is redirecting to {controller}/{action} with right json data instead of start {OnComplete = "Add_OnComplete"}:
View:
<script src="#Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>
<script src="#Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="#Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
#using (Ajax.BeginForm("Create", new AjaxOptions { OnComplete = "Add_OnComplete"}))
{
<fieldset>
<legend>Add</legend>
<div class="editor-label">
#Html.LabelFor(model => model.FullName)
</div>
<div class="editor-field">
#Html.TextBoxFor(model => model.FullName)
#Html.ValidationMessageFor(model => model.FullName)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Email)
</div>
<div class="editor-field">
#Html.TextBoxFor(model => model.Email)
#Html.ValidationMessageFor(model => model.Email)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.PhoneNumber)
</div>
<div class="editor-field">
#Html.TextBoxFor(model => model.PhoneNumber)
#Html.ValidationMessageFor(model => model.PhoneNumber)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Status)
</div>
<div class="editor-field">
#Html.TextBoxFor(model => model.Status)
#Html.ValidationMessageFor(model => model.Status)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<script type="text/javascript">
function Add_OnComplete(context) {
var JsonAdd = context.get_response().get_object();
if (JsonAdd.Success) {
//TODO
}
}
</script>
Controller:
public PartialViewResult Create()
{
return PartialView();
}
[HttpPost]
public JsonResult Create(Subscribe model)
{
if (ModelState.IsValid)
{
_entity.CreateSubscribe(model, SubscribeStatus.Customer.GetHashCode());
var message = new Message(MailSubject, MailBody, model.Email);
message.Send();
}
return Json(new
{
Success = true,
Message = "The person has been added!"
});
}
What I do wrong ? And what am I missing?
Thanks.
Remove that get_response().get_object() stuff.
Use just context.Success

Resources