Strange behaviour with MVC posting list - asp.net-mvc

I have the following dynamically created textboxes in my view:
#for (int i = 0; i < Model.MullionList.Count; i++)
{
ItemDrops curMullItem = Model.MullionList.ElementAt(i);
<div class="form-group">
#Html.Label(curMullItem.ItemName.ToString(), new { #class = "col-sm-6 control-label" })
<div class="col-sm-6">
#Html.TextBoxFor(x => x.MullionList[i].ItemPossInfo, new { #class = "form-control" })
#Html.HiddenFor(x => x.MullionList[i].ItemName)
</div>
</div>
}
I noticed occaionally for certain types of product this was returning null when posted (for MullionList), even though i selected these dropdowns.
so it works for certain product but not others.
The even weirder part is when i remove this section further down the view it works (ie MulionList is not null when the form is posted)
<div class="form-group">
#Html.Label("Glass", new { #class = "col-sm-6 control-label", id = "glass-first-label" })
<div class="col-sm-6">
#Html.DropDownListFor(gu => gu.GlassItems[0].Value, Model.GlassTypes, "-- Select --", new { #class = "form-control glass-multi-select", id = "Glass" + 0 })
</div>
</div>
<div id="hidden-glass-select" style="display: none">
#for (int i = 1; i < Model.GlassUnitsCount; i++)
{
var glassUnit = Model.GlassUnits.ElementAt(i);
<div class="form-group">
#Html.Label(glassUnit.ToString(), new { #class = "col-sm-6 control-label" })
<div class="col-sm-6">
#Html.DropDownListFor(gu => gu.GlassItems[i].Value, Model.GlassTypes, "-- Select --", new { #class = "form-control glass-multi-select", id = "Glass" + i })
</div>
</div>
}
</div>
<div class="form-group">
#Html.LabelFor(go => go.GlazzingInfoVal, "Glazzing Option", new { #class = "col-sm-6 control-label" })
<div class="col-sm-6">
#Html.DropDownListFor(go => go.GlazzingInfoVal, Model.GlazzingOptions, "-- Select --", new { #class = "form-control" })
</div>
</div>

I think I have fixed this now.
I renamed all items (lists that started with Glass or Glazzing and it works!)
This looks like a proper MVC bug
Thanks to this answer:https://stackoverflow.com/a/16113919/66975
This has costed me several hours, GRRRR

Related

Using MVC HtmlHelpers, how to send a form with a value from database along with User-filled values?

I'm trying to write a Bank Application. In the Transaction Page, I want to take the value of CheckingAccountId from the User Accounts Database (User.Identity.GetUserId()). But, how should I assign the value of User.Identity.GetUserId() to CheckingAccountId. This is the relevant Razor View Page Code:
using (Html.BeginForm("Deposit", "Transaction", FormMethod.Post, new {
#class = "form-horizontal", role = "form" }))
{
#Html.AntiForgeryToken();
<h4>Deposit Section</h4>
<hr />
#Html.ValidationSummary("", new { #class = "text-danger" })
<div class="form-group">
#Html.LabelFor(m => m.Id, new { #class = "col-md-2 control-label" })
<div class="col-md-10">
#Html.TextBoxFor(m => m.Id, new { #class = "form-control" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(m => m.TransactionAmount, new { #class = "col-md-2 control-label" })
<div class="col-md-10">
#Html.TextBoxFor(m => m.TransactionAmount, new { #class = "form-control" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(m => m.CheckingAccountId, new { #class = "col-md-2 control-label" })
<div class="col-md-10">
#{
var userName = User.Identity.GetUserName();
}
#*THIS IS THE BEST I COULD DO. I NEED HELP HERE*#
#*#m.CheckingAccountId = userName*#
#Html.DisplayName(userName)**
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" class="btn btn-default" value="Deposit" />
</div>
</div>
UPDATE:
I just found out that we can achieve this by adding the same in the ConnectionString when writing the SQL Query for pushing the transaction into the database. The solution would look something like this:
INSERT INTO table ( checkingaccountid, model_data ) VALUES ( $id, $data );
But I would still like to know if it's possible to send this id from the form when submitted along with the other data.

Bootstrap form labels not placed before the textbox?

I'm not seeing why the following code is putting the labels to the right of the textbox instead of the to the left? Should I be using <div class="row"> around the <div class="form-group">?
Anyone know what I'm missing?
<div class="form-horizontal">
#using (Html.BeginForm("EditClockGroup", "Admin", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
#Html.ValidationSummary(false, "", new { #class = "alert alert-danger" })
#Html.HiddenFor(model => model.GroupId, null)
<div class="form-group">
#Html.LabelFor(m => m.GroupId, new { #class = "col-md-2 control-label" })
#Html.TextBoxFor(m => m.GroupId, new { #class = "col-md-7 form-control" })
</div>
<div class="form-group">
#Html.LabelFor(m => m.GroupName, new { #class = "col-md-2 control-label" })
#Html.TextBoxFor(m => m.GroupName, new { #class = "col-md-7 form-control" })
</div>
<div class="form-group">
<div class="col-md-7 col-md-offset-2">
<input type="submit" value="Update" class="btn btn-primary" />
</div>
</div>
}
</div>
You cannot apply col-md-7 to input tag. Instead, you need to place input inside div.
<div class="form-group">
#Html.LabelFor(m => m.GroupId, new { #class = "col-md-2 control-label" })
<div class="col-md-7">
#Html.TextBoxFor(m => m.GroupId, new { #class = "form-control" })
</div>
</div>

Parser error inside razor template

I am trying to use Captcha inside my project but get a parser error with the following code...struggling to eliminate it
#{MvcCaptcha registrationCaptcha = CaptchaHelper.GetRegistrationCaptcha(); }
#if (!registrationCaptcha.IsSolved)
{
<div class="form-group">
<div class="col-md-offset-4 col-md-8">
#Html.Captcha(registrationCaptcha)
</div>
#Html.Label("Retype the code", new { #class = "col-md-4 control-label", #for = "CaptchaCode" })
<div class="col-md-8">
#Html.TextBox("CaptchaCode", null, new { #class = "form-control captchaVal" })
</div>
</div>
}
What would the cause be of the parser error?
managed to rectify it
MvcCaptcha registrationCaptcha = CaptchaHelper.GetRegistrationCaptcha();
if (!registrationCaptcha.IsSolved)
{
<div class="form-group">
<div class="col-md-offset-4 col-md-8">
#Html.Captcha(registrationCaptcha)
</div>
#Html.Label("Retype the code",
new { #class = "col-md-4 control-label", #for = "CaptchaCode" })
<div class="col-md-8">
#Html.TextBox("CaptchaCode", null,
new { #class = "form-control captchaVal" })
</div>
</div>
}

Select field in form

I am using a form like this, how can I change the textboxforinto a select, because I can't find any option to do it in the #Html
#using (Html.BeginForm("LeggTilUtdannelse", "Utdannelse", FormMethod.Post, new { #class = "form-horizontal", id = "legg-til-form" }))
{
#Html.AntiForgeryToken()
<div class="form-group">
#Html.LabelFor(m => m.Til, new { #class = "col-md-2 control-label" })
<div class="col-md-10">
#Html.TextBoxFor(m => m.Til, new { #class = "form-control", autocomplete = "off" })
</div>
</div>
}
I think you're looking for an #Html.DropDownList or #Html.DropDownListFor.

</br> tag not working in MVC view

Not sure if this is a MVC or Razor problem. But I can't put a line break in my view. This is the code on the index.cshtml
#using (Html.BeginForm("Create", "Vacant", FormMethod.Post, new { role = "form" }))
{
#Html.AntiForgeryToken()
#Html.ValidationSummary("", new { #class = "text-danger" })
<!--TITLE-->
<div class="form-group">
#Html.LabelFor(m => m.Title, new { #class = "col-md-3 control-label" })
<div class="col-md-12">
#Html.TextBoxFor(m => m.Title, new { #class = "form-control" })
</div>
</div>
<!--DESCRIPTION-->
<div class="form-group">
#Html.LabelFor(m => m.Description, new { #class = "col-md-3 control-label" })
<div class="col-md-10">
#Html.TextAreaFor(m => m.Description, new { #class = "form-control" })
</div>
</div>
<br /> ----> HERE
<!--TABS-->
<div class="form-group">
<div class="col-md-10">
As you can see I put the line break tag before to displayed the third field (Project) in my View.
The View is displayed without any error. However, the line break is not rendered between the 2nd and 3rd fields. Causing those two fields one after another with any space on them.
This is the screenshot
How could I put a line break between those two fields? Is there any #Html helper or Razor code to do that?
Thanks!
Try this
<div style="clear:both;margin-bottom:20px">
<!--TABS-->
<div class="form-group">
You can insert one more Div Between them.
OR
You can decorate the TAB Div with some STYLE like-
<div class="form-group" style="margin-top:20px">
OR
You can insert one more <P /> Between them.

Resources