Building Razor view using foreach and switch - asp.net-mvc

I'm trying to recreate this in Razor with a foreach and switch and having difficulties. It seems that opening and closing DIVs cause issue in the Razor
code and it keeps saying either I don't have a closing } on the foreach (var questionBasicSection in Model.ToList()) or the </div> in the last case doesn't have a matching start tag.
I have tried using <text></text> tags around the <div class="row mt5"> <!-- #question.QUESTION --> tags but can't use it
around the ` tags.
Any ideas as to how to accomplish this?
Because the sections are different, it really has to be formatted this way...
Here's the desired HTML output:
<div class="row mt5"> <!-- Personnel -->
<div class="col-md-4">
<label class="control-label col-med-6" for="QuestionBasicSection.Questions[0].ANSWER_FLOAT">a. Personnel</label>
</div>
<div class="col-md-2">
</div>
<div class="col-md-3">
<input name="QuestionBasicSection.Questions[0].AnswerFloatString" class="form-control currencyMask amt" data-bind="textInput: sdto.DATE_INACTIVE"
data-pattern="currency" id="Personnel_Amount_Budgeted" type="text" value="" />
<input type="hidden" name="QuestionBasicSection.Questions[0].ANSWER_DATA_TYPE" id="QuestionBasicSection.Questions[0].ANSWER_DATA_TYPE" value="4" />
<input type="hidden" name="QuestionBasicSection.Questions[0].ID" id="QuestionBasicSection.Questions[0].ID" value="200" />
<input type="hidden" name="QuestionBasicSection.Questions[0].IS_MULTI_ANSWER" id="QuestionBasicSection.Questions[0].IS_MULTI_ANSWER" value="False" />
<input type="hidden" name="QuestionBasicSection.Questions[0].QUESTION_SECTION_ID" id="QuestionBasicSection.Questions[0].QUESTION_SECTION_ID" value="9" />
</div>
<div class="col-md-3">
<input name="QuestionBasicSection.Questions[1].AnswerFloatString" class="form-control percentMask" data-bind="textInput: sdto.DATE_INACTIVE"
data-pattern="percent" id="Personnel_Amount_Budgeted_Pct" type="text" value="" disabled />
<input type="hidden" name="QuestionBasicSection.Questions[1].ANSWER_DATA_TYPE" id="QuestionBasicSection.Questions[1].ANSWER_DATA_TYPE" value="4" />
<input type="hidden" name="QuestionBasicSection.Questions[1].ID" id="QuestionBasicSection.Questions[1].ID" value="201" />
<input type="hidden" name="QuestionBasicSection.Questions[1].IS_MULTI_ANSWER" id="QuestionBasicSection.Questions[1].IS_MULTI_ANSWER" value="False" />
<input type="hidden" name="QuestionBasicSection.Questions[1].QUESTION_SECTION_ID" id="QuestionBasicSection.Questions[1].QUESTION_SECTION_ID" value="9" />
</div>
</div>
<div class="row mt5"> <!-- Equipment -->
<div class="col-md-3">
<input name="QuestionBasicSection.Questions[2].AnswerFloatString" class="form-control currencyMask amt" data-bind="textInput: sdto.DATE_INACTIVE"
data-pattern="currency" id="Fringe_Benefits_Budgeted" type="text" value="" />
<input type="hidden" name="QuestionBasicSection.Questions[2].ANSWER_DATA_TYPE" id="QuestionBasicSection.Questions[2].ANSWER_DATA_TYPE" value="4" />
<input type="hidden" name="QuestionBasicSection.Questions[2].ID" id="QuestionBasicSection.Questions[2].ID" value="202" />
<input type="hidden" name="QuestionBasicSection.Questions[2].IS_MULTI_ANSWER" id="QuestionBasicSection.Questions[2].IS_MULTI_ANSWER" value="False" />
<input type="hidden" name="QuestionBasicSection.Questions[2].QUESTION_SECTION_ID" id="QuestionBasicSection.Questions[2].QUESTION_SECTION_ID" value="9" />
</div>
<div class="col-md-2">
<input name="QuestionBasicSection.Questions[3].AnswerFloatString" class="form-control percentMask_2" data-bind="textInput: sdto.DATE_INACTIVE"
data-pattern="percent" id="Fringe_Benefits_Pct" type="text" value="" disabled />
<input type="hidden" name="QuestionBasicSection.Questions[3].ANSWER_DATA_TYPE" id="QuestionBasicSection.Questions[3].ANSWER_DATA_TYPE" value="4" />
<input type="hidden" name="QuestionBasicSection.Questions[3].ID" id="QuestionBasicSection.Questions[3].ID" value="203" />
<input type="hidden" name="QuestionBasicSection.Questions[3].IS_MULTI_ANSWER" id="QuestionBasicSection.Questions[3].IS_MULTI_ANSWER" value="False" />
<input type="hidden" name="QuestionBasicSection.Questions[3].QUESTION_SECTION_ID" id="QuestionBasicSection.Questions[3].QUESTION_SECTION_ID" value="9" />
</div>
<div class="col-md-3">
<input name="QuestionBasicSection.Questions[4].AnswerFloatString" class="form-control percentMask" data-bind="textInput: sdto.DATE_INACTIVE"
data-pattern="currency" id="Fringe_Benefits_Budgeted_Pct" type="text" value="" disabled />
<input type="hidden" name="QuestionBasicSection.Questions[4].ANSWER_DATA_TYPE" id="QuestionBasicSection.Questions[4].ANSWER_DATA_TYPE" value="4" />
<input type="hidden" name="QuestionBasicSection.Questions[4].ID" id="QuestionBasicSection.Questions[4].ID" value="204" />
<input type="hidden" name="QuestionBasicSection.Questions[4].IS_MULTI_ANSWER" id="QuestionBasicSection.Questions[4].IS_MULTI_ANSWER" value="False" />
<input type="hidden" name="QuestionBasicSection.Questions[4].QUESTION_SECTION_ID" id="QuestionBasicSection.Questions[4].QUESTION_SECTION_ID" value="9" />
</div>
</div>
<div class="row mt5"> <!-- Travel-->
<div class="col-md-2">
</div>
<div class="col-md-3">
<input name="QuestionBasicSection.Questions[5].AnswerFloatString" class="form-control currencyMask amt" data-bind="textInput: sdto.DATE_INACTIVE"
data-pattern="currency" id="Travel_Budgeted" type="text" value="" />
<input type="hidden" name="QuestionBasicSection.Questions[5].ANSWER_DATA_TYPE" id="QuestionBasicSection.Questions[5].ANSWER_DATA_TYPE" value="4" />
<input type="hidden" name="QuestionBasicSection.Questions[5].ID" id="QuestionBasicSection.Questions[5].ID" value="205" />
<input type="hidden" name="QuestionBasicSection.Questions[5].IS_MULTI_ANSWER" id="QuestionBasicSection.Questions[5].IS_MULTI_ANSWER" value="False" />
<input type="hidden" name="QuestionBasicSection.Questions[5].QUESTION_SECTION_ID" id="QuestionBasicSection.Questions[5].QUESTION_SECTION_ID" value="9" />
</div>
<div class="col-md-3">
<input name="QuestionBasicSection.Questions[6].AnswerFloatString" class="form-control percentMask" data-bind="textInput: sdto.DATE_INACTIVE"
data-pattern="percent" id="Travel_Budgeted_Pct" type="text" value="" disabled />
<input type="hidden" name="QuestionBasicSection.Questions[6].ANSWER_DATA_TYPE" id="QuestionBasicSection.Questions[6].ANSWER_DATA_TYPE" value="4" />
<input type="hidden" name="QuestionBasicSection.Questions[6].ID" id="QuestionBasicSection.Questions[6].ID" value="206" />
<input type="hidden" name="QuestionBasicSection.Questions[6].IS_MULTI_ANSWER" id="QuestionBasicSection.Questions[6].IS_MULTI_ANSWER" value="False" />
<input type="hidden" name="QuestionBasicSection.Questions[6].QUESTION_SECTION_ID" id="QuestionBasicSection.Questions[6].QUESTION_SECTION_ID" value="9" />
</div>
</div>
Here's the Razor code:
#foreach (var questionBasicSection in Model.ToList())
{
#* doing some other stuff here *#
foreach (var question in questionBasicSection.Questions)
{
var QuestionTitle = (question.DISPLAY_NAME != null?question.DISPLAY_NAME + " " + question.QUESTION: question.QUESTION);
switch (question.QuestionIndex)
{
case 200:
<text><div class="row mt5"> <!-- #question.QUESTION --></text>
<div class="col-md-4">
<label class="control-label col-med-6" for="QuestionBasicSection.Questions[0].ANSWER_FLOAT">#QuestionTitle/label>
</div>
<div class="col-md-2">
</div>
<div class="col-md-3">
#Html.TextBoxFor(q => question.AnswerFloatString, (object)new { disabled = "disabled" } : new { id=question.QUESTION.Replace(' ', '_').Replace(".", String.Empty).Replace("=", String.Empty),
name = "QuestionBasicSection.Questions[" + question.Index + "].AnswerFloatString", #class = "form-control floatwidth currencyMask amt",
data_bind = "textInput: sdto.DATE_INACTIVE", data_pattern = question.FORMAT_VALIDATION })
<input type="hidden" name="QuestionBasicSection.Questions[#question.Index].ANSWER_DATA_TYPE" id="QuestionBasicSection.Questions[#question.Index].ANSWER_DATA_TYPE" value="#question.ANSWER_DATA_TYPE" />
<input type="hidden" name="QuestionBasicSection.Questions[#question.Index].ID" id="QuestionBasicSection.Questions[#question.Index].ID" value="#question.ID" />
<input type="hidden" name="QuestionBasicSection.Questions[#question.Index].IS_MULTI_ANSWER" id="QuestionBasicSection.Questions[#question.Index].IS_MULTI_ANSWER" value="#question.IS_MULTI_ANSWER.ToString()" />
<input type="hidden" name="QuestionBasicSection.Questions[#question.Index].QUESTION_SECTION_ID" id="QuestionBasicSection.Questions[#question.Index].QUESTION_SECTION_ID" value="#question.QUESTION_SECTION_ID.ToString()" />
</div>
break;
case 201:
<div class="col-md-3">
#Html.TextBoxFor(q => question.AnswerFloatString, (object)new { disabled = "disabled" } : new { id=question.QUESTION.Replace(' ', '_').Replace(".", String.Empty).Replace("=", String.Empty),
name = "QuestionBasicSection.Questions[" + question.Index + "].AnswerFloatString", #class = "form-control floatwidth percentMask",
data_bind = "textInput: sdto.DATE_INACTIVE", data_pattern = question.FORMAT_VALIDATION })
<input type="hidden" name="QuestionBasicSection.Questions[#question.Index].ANSWER_DATA_TYPE" id="QuestionBasicSection.Questions[#question.Index].ANSWER_DATA_TYPE" value="#question.ANSWER_DATA_TYPE" />
<input type="hidden" name="QuestionBasicSection.Questions[#question.Index].ID" id="QuestionBasicSection.Questions[#question.Index].ID" value="#question.ID" />
<input type="hidden" name="QuestionBasicSection.Questions[#question.Index].IS_MULTI_ANSWER" id="QuestionBasicSection.Questions[#question.Index].IS_MULTI_ANSWER" value="#question.IS_MULTI_ANSWER.ToString()" />
<input type="hidden" name="QuestionBasicSection.Questions[#question.Index].QUESTION_SECTION_ID" id="QuestionBasicSection.Questions[#question.Index].QUESTION_SECTION_ID" value="#question.QUESTION_SECTION_ID.ToString()" />
</div>
</div>
break;
case 202:
<text><div class="row mt5"> <!-- #question.QUESTION --></text>
<div class="col-md-4">
<label class="control-label col-med-6" for="QuestionBasicSection.Questions[0].ANSWER_FLOAT">#QuestionTitle/label>
</div>
<div class="col-md-3">
#Html.TextBoxFor(q => question.AnswerFloatString, (object)new { disabled = "disabled" } : new { id=question.QUESTION.Replace(' ', '_').Replace(".", String.Empty).Replace("=", String.Empty),
name = "QuestionBasicSection.Questions[" + question.Index + "].AnswerFloatString", #class = "form-control floatwidth currencyMask amt",
data_bind = "textInput: sdto.DATE_INACTIVE", data_pattern = question.FORMAT_VALIDATION })
<input type="hidden" name="QuestionBasicSection.Questions[#question.Index].ANSWER_DATA_TYPE" id="QuestionBasicSection.Questions[#question.Index].ANSWER_DATA_TYPE" value="#question.ANSWER_DATA_TYPE" />
<input type="hidden" name="QuestionBasicSection.Questions[#question.Index].ID" id="QuestionBasicSection.Questions[#question.Index].ID" value="#question.ID" />
<input type="hidden" name="QuestionBasicSection.Questions[#question.Index].IS_MULTI_ANSWER" id="QuestionBasicSection.Questions[#question.Index].IS_MULTI_ANSWER" value="#question.IS_MULTI_ANSWER.ToString()" />
<input type="hidden" name="QuestionBasicSection.Questions[#question.Index].QUESTION_SECTION_ID" id="QuestionBasicSection.Questions[#question.Index].QUESTION_SECTION_ID" value="#question.QUESTION_SECTION_ID.ToString()" />
</div>
break;
case 203:
<div class="col-md-2">
#Html.TextBoxFor(q => question.AnswerFloatString, (object)new { disabled = "disabled" } : new { id=question.QUESTION.Replace(' ', '_').Replace(".", String.Empty).Replace("=", String.Empty),
name = "QuestionBasicSection.Questions[" + question.Index + "].AnswerFloatString", #class = "form-control floatwidth percentMask",
data_bind = "textInput: sdto.DATE_INACTIVE", data_pattern = question.FORMAT_VALIDATION })
<input type="hidden" name="QuestionBasicSection.Questions[#question.Index].ANSWER_DATA_TYPE" id="QuestionBasicSection.Questions[#question.Index].ANSWER_DATA_TYPE" value="#question.ANSWER_DATA_TYPE" />
<input type="hidden" name="QuestionBasicSection.Questions[#question.Index].ID" id="QuestionBasicSection.Questions[#question.Index].ID" value="#question.ID" />
<input type="hidden" name="QuestionBasicSection.Questions[#question.Index].IS_MULTI_ANSWER" id="QuestionBasicSection.Questions[#question.Index].IS_MULTI_ANSWER" value="#question.IS_MULTI_ANSWER.ToString()" />
<input type="hidden" name="QuestionBasicSection.Questions[#question.Index].QUESTION_SECTION_ID" id="QuestionBasicSection.Questions[#question.Index].QUESTION_SECTION_ID" value="#question.QUESTION_SECTION_ID.ToString()" />
</div>
break;
case 204:
<div class="col-md-3">
#Html.TextBoxFor(q => question.AnswerFloatString, (object)new { disabled = "disabled" } : new { id=question.QUESTION.Replace(' ', '_').Replace(".", String.Empty).Replace("=", String.Empty),
name = "QuestionBasicSection.Questions[" + question.Index + "].AnswerFloatString", #class = "form-control floatwidth percentMask",
data_bind = "textInput: sdto.DATE_INACTIVE", data_pattern = question.FORMAT_VALIDATION })
<input type="hidden" name="QuestionBasicSection.Questions[#question.Index].ANSWER_DATA_TYPE" id="QuestionBasicSection.Questions[#question.Index].ANSWER_DATA_TYPE" value="#question.ANSWER_DATA_TYPE" />
<input type="hidden" name="QuestionBasicSection.Questions[#question.Index].ID" id="QuestionBasicSection.Questions[#question.Index].ID" value="#question.ID" />
<input type="hidden" name="QuestionBasicSection.Questions[#question.Index].IS_MULTI_ANSWER" id="QuestionBasicSection.Questions[#question.Index].IS_MULTI_ANSWER" value="#question.IS_MULTI_ANSWER.ToString()" />
<input type="hidden" name="QuestionBasicSection.Questions[#question.Index].QUESTION_SECTION_ID" id="QuestionBasicSection.Questions[#question.Index].QUESTION_SECTION_ID" value="#question.QUESTION_SECTION_ID.ToString()" />
</div>
</div>
break;
case 205:
<text><div class="row mt5"> <!-- #question.QUESTION --></text>
<div class="col-md-4">
<label class="control-label col-med-6" for="QuestionBasicSection.Questions[0].ANSWER_FLOAT">#QuestionTitle/label>
</div>
<div class="col-md-2">
</div>
<div class="col-md-3">
#Html.TextBoxFor(q => question.AnswerFloatString, (object)new { disabled = "disabled" } : new { id=question.QUESTION.Replace(' ', '_').Replace(".", String.Empty).Replace("=", String.Empty),
name = "QuestionBasicSection.Questions[" + question.Index + "].AnswerFloatString", #class = "form-control floatwidth currencyMask amt",
data_bind = "textInput: sdto.DATE_INACTIVE", data_pattern = question.FORMAT_VALIDATION })
<input type="hidden" name="QuestionBasicSection.Questions[#question.Index].ANSWER_DATA_TYPE" id="QuestionBasicSection.Questions[#question.Index].ANSWER_DATA_TYPE" value="#question.ANSWER_DATA_TYPE" />
<input type="hidden" name="QuestionBasicSection.Questions[#question.Index].ID" id="QuestionBasicSection.Questions[#question.Index].ID" value="#question.ID" />
<input type="hidden" name="QuestionBasicSection.Questions[#question.Index].IS_MULTI_ANSWER" id="QuestionBasicSection.Questions[#question.Index].IS_MULTI_ANSWER" value="#question.IS_MULTI_ANSWER.ToString()" />
<input type="hidden" name="QuestionBasicSection.Questions[#question.Index].QUESTION_SECTION_ID" id="QuestionBasicSection.Questions[#question.Index].QUESTION_SECTION_ID" value="#question.QUESTION_SECTION_ID.ToString()" />
</div>
break;
case 206:
<div class="col-md-3">
#Html.TextBoxFor(q => question.AnswerFloatString, (object)new { disabled = "disabled" } : new { id=question.QUESTION.Replace(' ', '_').Replace(".", String.Empty).Replace("=", String.Empty),
name = "QuestionBasicSection.Questions[" + question.Index + "].AnswerFloatString", #class = "form-control floatwidth percentMask",
data_bind = "textInput: sdto.DATE_INACTIVE", data_pattern = question.FORMAT_VALIDATION })
<input type="hidden" name="QuestionBasicSection.Questions[#question.Index].ANSWER_DATA_TYPE" id="QuestionBasicSection.Questions[#question.Index].ANSWER_DATA_TYPE" value="#question.ANSWER_DATA_TYPE" />
<input type="hidden" name="QuestionBasicSection.Questions[#question.Index].ID" id="QuestionBasicSection.Questions[#question.Index].ID" value="#question.ID" />
<input type="hidden" name="QuestionBasicSection.Questions[#question.Index].IS_MULTI_ANSWER" id="QuestionBasicSection.Questions[#question.Index].IS_MULTI_ANSWER" value="#question.IS_MULTI_ANSWER.ToString()" />
<input type="hidden" name="QuestionBasicSection.Questions[#question.Index].QUESTION_SECTION_ID" id="QuestionBasicSection.Questions[#question.Index].QUESTION_SECTION_ID" value="#question.QUESTION_SECTION_ID.ToString()" />
</div>
</div>
break;
}
}
}

Easier to show you in code:
#foreach (var questionBasicSection in Model.ToList())
{
// New section start
<div class="row mt5"><span>#question.QUESTION</span>
#* doing some other stuff here *#
foreach (var question in questionBasicSection.Questions)
{
var QuestionTitle = (question.DISPLAY_NAME != null?question.DISPLAY_NAME + " " + question.QUESTION: question.QUESTION);
switch (question.QuestionIndex)
{
case 200:
<div class="col-md-4">
<label class="control-label col-med-6" for="QuestionBasicSection.Questions[0].ANSWER_FLOAT">#QuestionTitle/label>
</div>
<div class="col-md-2">
</div>
<div class="col-md-3">
#Html.TextBoxFor(q => question.AnswerFloatString, (object)new { disabled = "disabled" } : new { id=question.QUESTION.Replace(' ', '_').Replace(".", String.Empty).Replace("=", String.Empty),
name = "QuestionBasicSection.Questions[" + question.Index + "].AnswerFloatString", #class = "form-control floatwidth currencyMask amt",
data_bind = "textInput: sdto.DATE_INACTIVE", data_pattern = question.FORMAT_VALIDATION })
<input type="hidden" name="QuestionBasicSection.Questions[#question.Index].ANSWER_DATA_TYPE" id="QuestionBasicSection.Questions[#question.Index].ANSWER_DATA_TYPE" value="#question.ANSWER_DATA_TYPE" />
<input type="hidden" name="QuestionBasicSection.Questions[#question.Index].ID" id="QuestionBasicSection.Questions[#question.Index].ID" value="#question.ID" />
<input type="hidden" name="QuestionBasicSection.Questions[#question.Index].IS_MULTI_ANSWER" id="QuestionBasicSection.Questions[#question.Index].IS_MULTI_ANSWER" value="#question.IS_MULTI_ANSWER.ToString()" />
<input type="hidden" name="QuestionBasicSection.Questions[#question.Index].QUESTION_SECTION_ID" id="QuestionBasicSection.Questions[#question.Index].QUESTION_SECTION_ID" value="#question.QUESTION_SECTION_ID.ToString()" />
</div>
break;
case 201:
<div class="col-md-3">
#Html.TextBoxFor(q => question.AnswerFloatString, (object)new { disabled = "disabled" } : new { id=question.QUESTION.Replace(' ', '_').Replace(".", String.Empty).Replace("=", String.Empty),
name = "QuestionBasicSection.Questions[" + question.Index + "].AnswerFloatString", #class = "form-control floatwidth percentMask",
data_bind = "textInput: sdto.DATE_INACTIVE", data_pattern = question.FORMAT_VALIDATION })
<input type="hidden" name="QuestionBasicSection.Questions[#question.Index].ANSWER_DATA_TYPE" id="QuestionBasicSection.Questions[#question.Index].ANSWER_DATA_TYPE" value="#question.ANSWER_DATA_TYPE" />
<input type="hidden" name="QuestionBasicSection.Questions[#question.Index].ID" id="QuestionBasicSection.Questions[#question.Index].ID" value="#question.ID" />
<input type="hidden" name="QuestionBasicSection.Questions[#question.Index].IS_MULTI_ANSWER" id="QuestionBasicSection.Questions[#question.Index].IS_MULTI_ANSWER" value="#question.IS_MULTI_ANSWER.ToString()" />
<input type="hidden" name="QuestionBasicSection.Questions[#question.Index].QUESTION_SECTION_ID" id="QuestionBasicSection.Questions[#question.Index].QUESTION_SECTION_ID" value="#question.QUESTION_SECTION_ID.ToString()" />
</div>
break;
case 202:
<div class="col-md-4">
<label class="control-label col-med-6" for="QuestionBasicSection.Questions[0].ANSWER_FLOAT">#QuestionTitle/label>
</div>
<div class="col-md-3">
#Html.TextBoxFor(q => question.AnswerFloatString, (object)new { disabled = "disabled" } : new { id=question.QUESTION.Replace(' ', '_').Replace(".", String.Empty).Replace("=", String.Empty),
name = "QuestionBasicSection.Questions[" + question.Index + "].AnswerFloatString", #class = "form-control floatwidth currencyMask amt",
data_bind = "textInput: sdto.DATE_INACTIVE", data_pattern = question.FORMAT_VALIDATION })
<input type="hidden" name="QuestionBasicSection.Questions[#question.Index].ANSWER_DATA_TYPE" id="QuestionBasicSection.Questions[#question.Index].ANSWER_DATA_TYPE" value="#question.ANSWER_DATA_TYPE" />
<input type="hidden" name="QuestionBasicSection.Questions[#question.Index].ID" id="QuestionBasicSection.Questions[#question.Index].ID" value="#question.ID" />
<input type="hidden" name="QuestionBasicSection.Questions[#question.Index].IS_MULTI_ANSWER" id="QuestionBasicSection.Questions[#question.Index].IS_MULTI_ANSWER" value="#question.IS_MULTI_ANSWER.ToString()" />
<input type="hidden" name="QuestionBasicSection.Questions[#question.Index].QUESTION_SECTION_ID" id="QuestionBasicSection.Questions[#question.Index].QUESTION_SECTION_ID" value="#question.QUESTION_SECTION_ID.ToString()" />
</div>
break;
case 203:
<div class="col-md-2">
#Html.TextBoxFor(q => question.AnswerFloatString, (object)new { disabled = "disabled" } : new { id=question.QUESTION.Replace(' ', '_').Replace(".", String.Empty).Replace("=", String.Empty),
name = "QuestionBasicSection.Questions[" + question.Index + "].AnswerFloatString", #class = "form-control floatwidth percentMask",
data_bind = "textInput: sdto.DATE_INACTIVE", data_pattern = question.FORMAT_VALIDATION })
<input type="hidden" name="QuestionBasicSection.Questions[#question.Index].ANSWER_DATA_TYPE" id="QuestionBasicSection.Questions[#question.Index].ANSWER_DATA_TYPE" value="#question.ANSWER_DATA_TYPE" />
<input type="hidden" name="QuestionBasicSection.Questions[#question.Index].ID" id="QuestionBasicSection.Questions[#question.Index].ID" value="#question.ID" />
<input type="hidden" name="QuestionBasicSection.Questions[#question.Index].IS_MULTI_ANSWER" id="QuestionBasicSection.Questions[#question.Index].IS_MULTI_ANSWER" value="#question.IS_MULTI_ANSWER.ToString()" />
<input type="hidden" name="QuestionBasicSection.Questions[#question.Index].QUESTION_SECTION_ID" id="QuestionBasicSection.Questions[#question.Index].QUESTION_SECTION_ID" value="#question.QUESTION_SECTION_ID.ToString()" />
</div>
break;
case 204:
<div class="col-md-3">
#Html.TextBoxFor(q => question.AnswerFloatString, (object)new { disabled = "disabled" } : new { id=question.QUESTION.Replace(' ', '_').Replace(".", String.Empty).Replace("=", String.Empty),
name = "QuestionBasicSection.Questions[" + question.Index + "].AnswerFloatString", #class = "form-control floatwidth percentMask",
data_bind = "textInput: sdto.DATE_INACTIVE", data_pattern = question.FORMAT_VALIDATION })
<input type="hidden" name="QuestionBasicSection.Questions[#question.Index].ANSWER_DATA_TYPE" id="QuestionBasicSection.Questions[#question.Index].ANSWER_DATA_TYPE" value="#question.ANSWER_DATA_TYPE" />
<input type="hidden" name="QuestionBasicSection.Questions[#question.Index].ID" id="QuestionBasicSection.Questions[#question.Index].ID" value="#question.ID" />
<input type="hidden" name="QuestionBasicSection.Questions[#question.Index].IS_MULTI_ANSWER" id="QuestionBasicSection.Questions[#question.Index].IS_MULTI_ANSWER" value="#question.IS_MULTI_ANSWER.ToString()" />
<input type="hidden" name="QuestionBasicSection.Questions[#question.Index].QUESTION_SECTION_ID" id="QuestionBasicSection.Questions[#question.Index].QUESTION_SECTION_ID" value="#question.QUESTION_SECTION_ID.ToString()" />
</div>
break;
case 205:
<div class="col-md-4">
<label class="control-label col-med-6" for="QuestionBasicSection.Questions[0].ANSWER_FLOAT">#QuestionTitle/label>
</div>
<div class="col-md-2">
</div>
<div class="col-md-3">
#Html.TextBoxFor(q => question.AnswerFloatString, (object)new { disabled = "disabled" } : new { id=question.QUESTION.Replace(' ', '_').Replace(".", String.Empty).Replace("=", String.Empty),
name = "QuestionBasicSection.Questions[" + question.Index + "].AnswerFloatString", #class = "form-control floatwidth currencyMask amt",
data_bind = "textInput: sdto.DATE_INACTIVE", data_pattern = question.FORMAT_VALIDATION })
<input type="hidden" name="QuestionBasicSection.Questions[#question.Index].ANSWER_DATA_TYPE" id="QuestionBasicSection.Questions[#question.Index].ANSWER_DATA_TYPE" value="#question.ANSWER_DATA_TYPE" />
<input type="hidden" name="QuestionBasicSection.Questions[#question.Index].ID" id="QuestionBasicSection.Questions[#question.Index].ID" value="#question.ID" />
<input type="hidden" name="QuestionBasicSection.Questions[#question.Index].IS_MULTI_ANSWER" id="QuestionBasicSection.Questions[#question.Index].IS_MULTI_ANSWER" value="#question.IS_MULTI_ANSWER.ToString()" />
<input type="hidden" name="QuestionBasicSection.Questions[#question.Index].QUESTION_SECTION_ID" id="QuestionBasicSection.Questions[#question.Index].QUESTION_SECTION_ID" value="#question.QUESTION_SECTION_ID.ToString()" />
</div>
break;
case 206:
<div class="col-md-3">
#Html.TextBoxFor(q => question.AnswerFloatString, (object)new { disabled = "disabled" } : new { id=question.QUESTION.Replace(' ', '_').Replace(".", String.Empty).Replace("=", String.Empty),
name = "QuestionBasicSection.Questions[" + question.Index + "].AnswerFloatString", #class = "form-control floatwidth percentMask",
data_bind = "textInput: sdto.DATE_INACTIVE", data_pattern = question.FORMAT_VALIDATION })
<input type="hidden" name="QuestionBasicSection.Questions[#question.Index].ANSWER_DATA_TYPE" id="QuestionBasicSection.Questions[#question.Index].ANSWER_DATA_TYPE" value="#question.ANSWER_DATA_TYPE" />
<input type="hidden" name="QuestionBasicSection.Questions[#question.Index].ID" id="QuestionBasicSection.Questions[#question.Index].ID" value="#question.ID" />
<input type="hidden" name="QuestionBasicSection.Questions[#question.Index].IS_MULTI_ANSWER" id="QuestionBasicSection.Questions[#question.Index].IS_MULTI_ANSWER" value="#question.IS_MULTI_ANSWER.ToString()" />
<input type="hidden" name="QuestionBasicSection.Questions[#question.Index].QUESTION_SECTION_ID" id="QuestionBasicSection.Questions[#question.Index].QUESTION_SECTION_ID" value="#question.QUESTION_SECTION_ID.ToString()" />
</div>
break;
} // end of questions for section
} //end of section
// Section end
</div>
}

You can use the #: syntax for each line of partial HTML code:
#if (foo)
{
#:</div>
}
However, I've still had issues with this when autoformatting on paste is enabled. As Razor re-evaluates the entire view and more often than not borks everything with improper indentation and not breaking lines like it should.
Probably your safest bet is to use Html.Raw:
#if (foo)
{
#Html.Raw("</div>")
}
Now, it's not even HTML code anymore. It's just a string so Razor will ignore it entirely.

Related

'IndexModel' does not contain a public instance or extension definition for 'GetEnumerator'

Let me preface by saying that I am new to C# and CSHTML. I created a data entry form that is supposed to post the info submitted into a SQL DB and show the list of data in a new page. I'm stuck on the following error. Any Help would be greatly appreciated
CS1579 foreach statement cannot operate on variables of type 'IndexModel' because 'IndexModel' does not contain a public instance or extension definition for 'GetEnumerator'
#page
#model sUAS_WebApp.Pages.IndexModel
#{
ViewData["Title"] = "sUAS Request Form";
}
#if (Model.errorMessage.Length > 0)
{
<div class='alert alert-warning alert-dismissible fade show' role='alert'>
<strong>#Model.errorMessage</strong>
<button type='button' class='btn-close' data-bs-dismiss='alert' aria-label='close'></button>
</div>
}
#foreach (var data in Model)
{
<form method="POST">
<div class="container" style="border: 1px solid lightgrey;">
<div class="col-100">
<div class="row">
<div class="row">
<div class="col-50">
<label for="Date">Date</label>
<input type="text" id="Date" name="Date" class="form-control" placeholder="01-JAN-2022" value="#data.sRequest.Date" />
<div class="row">
<div class="col-50">
<label for="RD">RD#</label>
<input type="text" id="RD" name="RD" class="form-control" placeholder="AB000000" value="#data.sRequest.RD" />
</div>
<div class="col-50">
<label for="EV">EV#</label>
<input type="text" id="EV" name="EV" class="form-control" placeholder="00000" value="#data.sRequest.EV" />
</div>
</div>
</div>
</div>
<label for="Requestor">Requestor</label>
<input type="text" id="Requestor" name="Requestor" class="form-control col-95" placeholder="Name / Star" value="#data.sRequest.Requestor" />
<label for="Location">Location</label>
<input type="text" id="Location" name="Location" class="form-control col-95" placeholder="542 W. 15th Street" value="#data.sRequest.Location" />
<div class="row">
<div class="col-50">
<label for="Unit_Piloting_Drone">Unit Piloting Drone</label>
<select id="Unit_Piloting_Drone" class="form-control" name="Unit_Piloting_Drone" value="#data.sRequest.Unit_Piloting_Drone">
<option value="select1"> </option>
<option value="techlab">TechLab</option>
<option value="maiu">auditor</option>
<option value="other">Forensics</option>
</select>
</div>
<div class="col-50">
<label for="Upload">Upload</label>
<select id="Upload" class="form-control" name="Upload" value="#data.sRequest.Upload">
<option value="select2"> </option>
<option value="UpYes">Yes</option>
<option value="UpNo">No</option>
</select>
</div>
<div class="col-50">
<label for="Evidence_com_ID">ID</label>
<input type="text" id="ID" name="ID" class="form-control" placeholder="Evidence.com ID" value="#data.sRequest.ID" />
</div>
<div class="col-50">
<label for="Refused">Refused</label>
<select id="Refused" class="form-control" name="Refused" value="#data.sRequest.Refused">
<option value="select3"> </option>
<option value="Yes">Yes</option>
<option value="No">No</option>
</select>
</div>
</div>
<label for="Call_Recieved_By"> Call Received By</label>
<input type="text" id="Call_Recieved_By" name="Call_Recieved_By" class="form-control" placeholder="Name / Star" value="#data.sRequest.Call_Recieved_By" />
<div class="col-95">
<label for="Refusal_Circumstances">Refusal Circumstances</label>
<textarea id="Refusal_Circumstances" name="Refusal_Circumstances" class="form-control" placeholder="Wind Gusts Over 30mph" style="height:200px" value="#data.sRequest.Refusal_Circumstances"></textarea>
</div>
<input type="submit" value="Submit" class="btn">
</div>
</div>
</div>
</form>
}
#if (Model.successMessage.Length > 0)
{
<div class='alert alert-warning alert-dismissible fade show' role='alert'>
<strong>#Model.successMessage</strong>
<button type='button' class='btn-close' data-bs-dismiss='alert' aria-label='close'></button>
</div>
}
<br />
<br />
<br />
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Data.SqlClient;
namespace sUAS_WebApp.Pages
{
public class IndexModel : PageModel
{
public sUASData sRequest = new sUASData();
public String errorMessage = "";
public String successMessage = "";
public void OnGet()
{
}
public void OnPost()
{
sRequest.Date = Request.Form["Date"];
sRequest.RD = Request.Form["RD"];
sRequest.EV = Request.Form["EV"];
sRequest.Requestor = Request.Form["Requestor"];
sRequest.Location = Request.Form["Location"];
sRequest.Call_Recieved_By = Request.Form["Call_Recieved_By"];
sRequest.Unit_Piloting_Drone = Request.Form["Unit_Piloting_Drone"];
sRequest.Refused = Request.Form["Refused"];
sRequest.Refusal_Circumstances = Request.Form["Refusal_Circumstances"];
sRequest.Upload = Request.Form["Upload"];
sRequest.ID = Request.Form["ID"];
if ((sRequest.Date.Length == 0 || sRequest.Requestor.Length == 0 || sRequest.Location.Length == 0 || sRequest.Call_Recieved_By.Length == 0 || sRequest.Unit_Piloting_Drone.Length == 0 || sRequest.Refused.Length == 0 || sRequest.Upload.Length == 0))
{
errorMessage = "Please fill in all required fields";
return;
}
//save the new request to the database
try
{
String connectionString = "Data Source=.\\sqlexpress;Initial Catalog=sUASDatabase;Integrated Security=True";
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
string sql = "INSERT INTO sUAS" +
"(Date, RD, EV, Requestor, Location, Call_Recieved_By, Unit_Piloting_Drone, Refused, Refusal_Circumstances, Upload, ID) VALUES " +
"(#Date, #RD, #EV, #Requestor, #Location, #Call_Recieved_By, #Unit_Piloting_Drone, #Refused, #Refusal_Circumstances, #Upload, #ID);";
using (SqlCommand command = new SqlCommand(sql, connection))
{
command.Parameters.AddWithValue("#Date", sRequest.Date);
command.Parameters.AddWithValue("#RD", sRequest.RD);
command.Parameters.AddWithValue("#EV", sRequest.EV);
command.Parameters.AddWithValue("#Requestor", sRequest.Requestor);
command.Parameters.AddWithValue("#Location", sRequest.Location);
command.Parameters.AddWithValue("#Call_Recieved_By", sRequest.Call_Recieved_By);
command.Parameters.AddWithValue("#Unit_Piloting_Drone", sRequest.Unit_Piloting_Drone);
command.Parameters.AddWithValue("#Refused", sRequest.Refused);
command.Parameters.AddWithValue("#Refusal_Circumstances", sRequest.Refusal_Circumstances);
command.Parameters.AddWithValue("#Upload", sRequest.Upload);
command.Parameters.AddWithValue("#ID", sRequest.ID);
command.ExecuteNonQuery();
}
}
}
catch (Exception ex)
{
errorMessage = ex.Message;
return;
}
sRequest.Date = ""; sRequest.Requestor = ""; sRequest.Location = ""; sRequest.Call_Recieved_By = ""; sRequest.Unit_Piloting_Drone = ""; sRequest.Refused = ""; sRequest.Upload = "";
successMessage = "Request Successfully Entered";
Response.Redirect("/RequestData");
}
}
}
Thanks

passing viewmodel and a multiple file upload's content of files to the controller

I'm trying to pass a viewmodel and a multiple files uplaoded using a file upload control to the controller. No matter what the files argument is passed as null.
How can the uploaded files be passed to the controller.
Here is my view
#model DMS.BO.ViewModels.UploadDocumentViewModel
<div class="content-wrapper">
<form id="frmAddService" action="#Url.Action("UploadDocuments", "Home")" method="post" enctype="multipart/form-data">
#if (TempData["Message"] != null)
{
<script type="text/javascript">
window.onload = function () {
alert("#TempData["Message"]");
};
</script>
}
<div class="row">
<div class="col-sm-6">
<label class="control-label " for="CompanyName">Company Name</label>
#* <input type="text" class="form-control required tobecleared" name="CompanyName" data-val-message="Please enter company name"
placeholder="Company Name*" />*#
<select class="form-control qf-level required" name="uploadvm.hostbo.CompanyId" id="CompanyId"
data-val-message="Please select a Company">
<option value="" selected disabled>Company*</option>
#foreach (var item in Model.companies)
{
<option value="#item.CompanyId">#item.CompanyName</option>
}
</select>
</div>
<div class="col-sm-4">
<label class="control-label " for="HostName">Host Ip</label>
<input type="text" class="form-control required tobecleared" data-action="#Url.Action("GetHostName", "Home")" id="HostIp" name="uploadvm.hostbo.HostIP" data-val-message="Please enter Host IP"
placeholder="Host IP*" />
</div>
</div>
<br />
<div class="row">
<div class="col-sm-4">
<label class="control-label " for="HostName">Host Name</label>
<input type="text" class="form-control required tobecleared" name="uploadvm.hostbo.HostName" id="HostName" data-val-message="Please enter Host Name"
placeholder="Host Name*" />
</div>
<div class="col-sm-1">
<label class="control-label " for="IsActive">Active</label>
<input type="checkbox" id="IsActive" class="form-control" name="uploadvm.hostbo.IsActive" value="true" />
</div>
<div class="col-sm-6">
<label class="control-label " for="ServiceName">Service Name</label>
#* <input type="text" class="form-control required tobecleared" name="ServiceName" data-val-message="Please enter service Name"
placeholder="Service Name*" />*#
<select class="form-control qf-level required" name="uploadvm.servicebo.ServiceId" id="ServiceName"
data-val-message="Please select a Service">
<option value="" selected disabled>Service*</option>
#foreach (var item in Model.services)
{
<option value="#item.ServiceId">#item.ServiceName</option>
}
</select>
</div>
</div>
<br />
<div class="row">
<div class="col-sm-4">
<label class="control-label " for="ServiceProcedureVersion">Document Version</label>
<input type="text" class="form-control required tobecleared" name="uploadvm.serviceprocedurebo.VersionNum" data-val-message="Please enter Document Version"
placeholder="Document Version*" />
</div>
<div class="col-sm-4">
<label class="control-label " for="ServiceProcedureDate">Document Date</label>
<input type="text" class="form-control required tobecleared service_procedure_date" name="uploadvm.serviceprocedurebo.Date" data-val-message="Please enter date"
placeholder="Document Date*" value="#DateTime.Today.ToLongDateString()" />
</div>
</div>
<br />
<div class="row">
<div class="col-sm-3">
<input style="display: inline" id="filesToUpload" type="file" name="files" multiple="multiple" />
</div>
<div class="col-sm-6">
<ul id="fileList">
<li></li>
</ul>
</div>
<div class="col-sm-2">
<label class="control-label"></label>
<input type="submit" name="submit" value="Submit" class="btn btn-primary" />
</div>
</div>
<br />
<div id="ExistingServices">
</div>
</form>
</div>
My controller
[HttpPost]
public ActionResult UploadDocuments(UploadDocumentViewModel uploadvm, IEnumerable<HttpPostedFileBase> files)
{
HostBL hostbl = new HostBL();
DocumentBL documentbl = new DocumentBL();
//CompanyBL companybl=new CompanyBL();
long hostid = hostbl.CheckIfHostExists(uploadvm.hostbo.HostIp, uploadvm.hostbo.CompanyId);
if (hostid == 0 || hostid == null)
hostid = hostbl.InsertHost(uploadvm);
uploadvm.hostbo.HostId = hostid;
long serviceprocedureid = hostbl.InsertDetails(uploadvm);
//var CompanyName = companybl.getCompanyName(uploadvm.companybo.CompanyId);
List<ProcedureDocumentBO> documents = new List<ProcedureDocumentBO>();
//iterating through multiple file collection
foreach (HttpPostedFileBase file in files)
{
//Checking file is available to save.
if (file != null)
{
ProcedureDocumentBO proceduredocumentbo = new ProcedureDocumentBO();
var InputFileName = Path.GetFileName(file.FileName);
var filename = uploadvm.companybo.CompanyName
+ "_" + uploadvm.hostbo.HostName
+ "_" + uploadvm.servicebo.ServiceName
+ "_" + DateTime.Now.ToString("yyyyMMddHHmmssfff")
+ "_" + InputFileName;
proceduredocumentbo.DocumentFileName = filename;
proceduredocumentbo.ServiceProcedureId = serviceprocedureid;
proceduredocumentbo.CreatedOn = System.DateTime.Today;
var ServerSavePath = Path.Combine(Server.MapPath("~/UploadedFiles/") + filename);
//Save file to server folder
file.SaveAs(ServerSavePath);
//assigning file uploaded status to ViewBag for showing message to user.
//ViewBag.UploadStatus = uploadvm.files.Count().ToString() + " files uploaded successfully.";
documents.Add(proceduredocumentbo);
//long documentid=documentbl.InsertDocument(uploadvm.documentbo);
}
}
uploadvm.documents = documents;
int res = documentbl.InsertDocuments(uploadvm);
//uploadvm.documents=
if (res <= 0)
{
TempData["Message"] = "Documents Not Uploaded";
}
else
{
TempData["Message"] = "Documents Uploaded";
}
UploadDocumentViewModel uploadvmm = new UploadDocumentViewModel();
CompanyBL companybl = new CompanyBL();
ServiceBL servicebl = new ServiceBL();
uploadvmm.companies = companybl.GetAllCompanies();
uploadvmm.services = servicebl.GetAllServices();
return View(uploadvmm);
}
The files argument passed is always null for me. How are the files passed?
public string writeFile(HttpPostedFileBase file, string fileName, string folder, HttpServerUtilityBase Server)
{
string FileName = fileName;
if (file != null && file.ContentLength > 0)
{
FileName = string.Format("{0}" + file.FileName.Trim('"'), DateTime.Now.Ticks);
string nameAndLocation = "~/" + folder + "/" + FileName;
file.SaveAs(Server.MapPath(nameAndLocation));
}
return FileName;
}
[HttpPost]
public string upload(Tunes tune)
{
var img = Request.Files["ImgFile"];
var mp3 = Request.Files["Mp3File"];
var image = writeFile(img, "", "TuneImages", Server);
var mp3Name = writeFile(mp3, "", "mp3", Server);
tune.mp3 = mp3Name;
tune.img = image;
db.Tunes.Add(tune);
db.SaveChanges();
return "Record Save Successfully";
}
You can loop on Request.Files, you can easily upload multiple files without binding view model. Request.Files["ImgFile"] Request.Files["Mp3File"]
ImgFile and Mp3File name of input type file

Html.BeginForm no longer creating action in OrchardCMS

I have recently rebuild a module DLL for an Orchard CMS project and one of the pages is not longer rendering form actions using BeginFormAntiForgeryPost.
The .cshtml file is:
#using System;
#using Orchard.Mvc.Html;
#{
var eventId = (int) Model.EventId;
string price = (string)Model.Price;
Orchard.ContentManagement.ContentItem contentItem = Model.Item;
string currencies = (string)Model.Currencies;
var pricesArray = price.Split(new Char[]{';'});
var currenciesArray = currencies.Split(new Char[]{';'});
bool allowCurrencyChange = true;
if(pricesArray.Length!=currenciesArray.Length){
allowCurrencyChange = false;
}
price = pricesArray[0];
string currency = currenciesArray[0];
}
<div class="prices-container">
<div class="more-info-link">
<a class="btn" href="#Url.Content("~"+Url.ItemDisplayUrl(contentItem))">
More Information
</a>
</div>
<div class="prices-box">
#if(Convert.ToDecimal(price)>0){
<p class="price">#currency #String.Format("{0:0,0.00}",price) <br /><span style="font-size:13px;">+ VAT</span></p>
}
else{
<p class="price" style="text-indent:-99999px;">#currency #String.Format("{0:0,0.00}",price)</p>
}
#using (Html.BeginFormAntiForgeryPost(Url.Action("Add", "ShoppingCart", new { id = eventId, area = "Events" }))) {
if(Convert.ToDecimal(price)>0){
<input type="hidden" name="currency" value="#currency" />
<input type="hidden" name="price" value="#price" />
if(allowCurrencyChange){
for(int i=0; i<pricesArray.Length;i++){
<input type="hidden" name="#(currenciesArray[i])_price" value="#(pricesArray[i])" />
}
}
<button class="btn btn-success" type="submit">#T("register")</button>
}
else{
<a href="/oforms/Contact-Us" class="btn btn-success" >#T("enquire")</a>
}
}
</div>
</div>
The output before the rebuild was:
<div class="prices-container" shape-id="13">
<div class="more-info-link" shape-id="13">
<a class="btn" href="/identify-the-right-talent" shape-id="13">
More Information
</a>
</div>
<div class="prices-box" shape-id="13">
<p class="price" shape-id="13">GBP 1197.50 <br shape-id="13"><span style="font-size:13px;" shape-id="13">+ VAT</span></p>
<form action="/Events/ShoppingCart/Add/13817" method="post" shape-id="13"> <input type="hidden" name="currency" value="GBP" shape-id="13">
<input type="hidden" name="price" value="1197.50" shape-id="13">
<input type="hidden" name="GBP_price" value="1197.50" shape-id="13">
<button class="btn btn-success" type="submit" shape-id="13">register</button>
<input name="__RequestVerificationToken" type="hidden" value="w10QfGWfU3Y2UNIWRVXtsqUxcSS/OX4GxWCAsdaWDMHV15LqkMpDcqQFYJ1lpbf7ZlNTP5ckVrWFtatRh2poJBTE9NxlXQRua8CNuXgVGtGnSr3lpWpt2KeAwNNhsvmZIiSSSZOuhwbXD08K7+H2fSkrFUAOKVX+eTarLBWdjg8jySGV+Q82oA37NMEBZrQXOWkq4TjEusC5ILqptA1fLZOR8qu0xuPlWrMf4Dli2kw=" shape-id="13">
</form>
</div>
</div>
And after rebuilding the module the FORM ACTION is no longer output:
<div class="prices-container" shape-id="13">
<div class="more-info-link" shape-id="13">
<a class="btn" href="/identify-the-right-talent" shape-id="13">
More Information
</a>
</div>
<div class="prices-box" shape-id="13">
<p class="price" shape-id="13">GBP 1197.50 <br shape-id="13"><span style="font-size:13px;" shape-id="13">+ VAT</span></p>
<form action="" method="post" shape-id="13">
<input type="hidden" name="currency" value="GBP" shape-id="13">
<input type="hidden" name="price" value="1197.50" shape-id="13">
<input type="hidden" name="GBP_price" value="1197.50" shape-id="13">
<button class="btn btn-success" type="submit" shape-id="13">register</button>
<input name="__RequestVerificationToken" type="hidden" value="LcZyoXchQ7IjEJJvxoFQzAYNVOk1ybC7KB/GzXkYdlE5n1zIy6fg3Dod8twQDLxA/cYvBwYfXYDWbNSq81dsq4UTSxevK1vXJxWGAWKOlt0dmt3si5RKYJbIXEplIScSoEBtr5Jaq95YsCe4jpdMe3QroYF5NGj9P8BhPKWbucHJiLyBJULc6ktYuRL0BFQk+oE9AaaYUUv6hyh20T/XTlPjl9uxalvJMTCIiDtSlZQ=" shape-id="13">
</form>
</div>
</div>
Why would the BeginFormAntiForgeryPost and/or Url.Action no longer output the action URL?

MVC file not loaded when submited

i have an issue file instance always null , below is the code:
[HttpPost]
public ActionResult Create(ExamRegisterationModel model, HttpPostedFileBase file)
{
if (file != null && file.ContentLength > 0)
{
string path = AppDomain.CurrentDomain.BaseDirectory + "uploads/";
string filename = Path.GetFileName(Request.Files[0].FileName);
Request.Files[0].SaveAs(Path.Combine(path, filename));
}
}
Any Idea how to upload the file?
And my View :
<div class="input-group">
<label for="file">Upload Receipt:</label>
<input type="file" name="File" id="file" />
<input type="submit" name="Submit" id="Submit" value="upload" />
</div>
add a wrapper element.
<form action="/Controller/Create" method="post" enctype="multipart/form-data">
<div class="input-group">
<label for="file">Upload Receipt:</label>
<input type="file" name="File" id="file" />
<input type="submit" name="Submit" id="Submit" value="upload" />
</div>
</form>
It works after doing the following change:
#using (Html.BeginForm(new { enctype = "multipart/form-data" }))

MVC File uploads collection is always null

This was working and now for reasons unknown my file collection is null
VIEW
#section termimalContent {
#using (Html.BeginForm("Add", "Terminals_Policies", FormMethod.Post, new { enctype = "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.SelectedItem, new SelectList(Model.PolicyType, "Value", "Text"),new {#class = "PackageDDL"})
#Html.ValidationMessageFor(model => model.SelectedItem)
</div>
#foreach (var pick in Model.PackageTypeItems)
{
<div class="editor-label">
<label for="#pick.Name">
#pick.Name:</label>
</div>
<div class="editor-field">
<input class="text-box single-line" type="file" name="#pick.Name.Trim()" id="#pick.Name.Trim()" data-val="#pick.IsRequired.ToString().ToLower()" data-val-required="Please select a file" />
#Html.ValidationMessage(pick.Name.Trim())
</div>
}
#Html.HiddenFor(model => model.GroupId)
#Html.HiddenFor(model => model.GroupName)
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
}
HTML Generated
<form action="/Terminals_Policies/Add/1" enctype="multipart/form-data" method="post"> <fieldset>
<legend>
<h2>
Create new policy</h2>
</legend>
<p>
<strong>Assigning devices to Node:</strong> Root</p>
<div class="editor-label">
<label for="PolicyName">PolicyName</label>
</div>
<div class="editor-field">
<input class="text-box single-line" data-val="true" data-val-length="Policy name cannot be longer than 50 characters." data-val-length-max="50" data-val-required="Please enter the policy name" id="PolicyName" name="PolicyName" type="text" value="" />
<span class="field-validation-valid" data-valmsg-for="PolicyName" data-valmsg-replace="true"></span>
</div>
<div class="editor-label">
<label for="PolicyType">PolicyType</label>
</div>
<div class="editor-field">
<select class="PackageDDL" data-val="true" data-val-number="The field SelectedItem must be a number." id="SelectedItem" name="SelectedItem"><option value="1">IT application</option>
<option value="3"> definition</option>
<option value="4">definition</option>
<option value="5">project</option>
<option value="6">relay schedules</option>
<option value="7">table data</option>
<option value="9">transfer definition</option>
<option value="10">firmware update request</option>
<option value="11"> firmware update request</option>
<option value="12">dat</option>
<option value="15"> firmware</option>
<option value="16"> hex</option>
<option value="17">project</option>
<option value="18">firmware</option>
</select>
<span class="field-validation-valid" data-valmsg-for="SelectedItem" data-valmsg-replace="true"></span>
</div>
<div class="editor-label">
<label for="IT application file ">
IT application file :</label>
</div>
<div class="editor-field">
<input class="text-box single-line" type="file" name="IT application file" id="IT application file" data-val="true" data-val-required="Please select a file" />
<span class="field-validation-valid" data-valmsg-for="IT application file" data-valmsg-replace="true"></span>
</div>
<input data-val="true" data-val-number="The field GroupId must be a number." data-val-required="The GroupId field is required." id="GroupId" name="GroupId" type="hidden" value="1" />
<input id="GroupName" name="GroupName" type="hidden" value="Root" />
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
</form>
Controller:
[HttpGet]
public ActionResult Add(int id)
{
_polAdd = CreatePolicyAddModel(id);
return View(_polAdd);
}
[HttpPost]
public ActionResult Add(IEnumerable<HttpPostedFileBase> files, vmPoliciesAdd model)
{
_policyLogic.AddPolicyFile(files,model.PolicyName,(int)model.SelectedItem, "FILE");
return View();
}
On the post action of controller the colelction is empty, can anyone see an obvious mistake
Naming issue:
<input class="text-box single-line" type="file" name="IT application file" id="IT application file" data-val="true" data-val-required="Please select a file" />
Should of course be:
<input class="text-box single-line" type="file" name="files" id="IT application file" data-val="true" data-val-required="Please select a file" />
So fix your Razor code and make sure you have applied correct name to your file input if you expect the model binder to be able to bind to an action argument called files:
<input class="text-box single-line" type="file" name="files" id="#pick.Name.Trim()" data-val="#pick.IsRequired.ToString().ToLower()" data-val-required="Please select a file" />
Ah an by the way id's cannot contain spaces. So you've got a broken HTML. The following seems wrong: id="#pick.Name.Trim()" as well.

Resources