MVC data validation for listboxfor not working - asp.net-mvc

I am having a bit of trouble with my form being validated even if my listboxfor is empty. This is the viewmodel im using:
public class CopyExcelReportSearchViewModel: IViewModel
{
[Required]
public DateRange DateRange { get; set; }
[Required]
public Guid CustomerId { get; set; }
[Required]
public List<Guid> ProjectIds { get; set; }
[Required]
public List<Guid> LocationsIds { get; set; }
public List<Customer> AvailableCustomers { get; set; }
public List<Project> AvailableProjects { get; set; }
public List<Location> AvailableLocations { get; set; }
public CopyExcelReportSearchViewModel()
{
ProjectIds = new List<Guid>();
LocationsIds = new List<Guid>();
AvailableCustomers = new List<Customer>();
AvailableProjects = new List<Project>();
AvailableLocations = new List<Location>();
}
}
Here is the html
#model Path.CopyExcelReportSearchViewModel
#{
var customerDropdown = Model.AvailableCustomers.Select(x => new SelectListItem
{
Text = x.Name,
Value = x.Id.ToString(),
Selected = Model.CustomerId == x.Id
});
var locationDropdown = Model.AvailableLocations.Select(x => new SelectListItem
{
Text = x.Name,
Value = x.Id.ToString(),
Selected = Model.LocationsIds.Any(y => y == x.Id)
});
var projectDropdown = Model.AvailableProjects.Select(x => new SelectListItem
{
Text = x.Name,
Value = x.Id.ToString(),
Selected = Model.ProjectIds.Any(y => y == x.Id)
});
}
<script>
var optionTemplate = [
'<option value="[VALUE]">[TEXT]</option>'
].join();
$(function () {
$('#CustomerId').change(function () {
var value = $(this).val();
$.ajax({
url: '#Url.Action("GetProjectsByCustomerId")?customerId=' + value,
datatype: 'json',
type: 'GET',
cache: false,
success: function (data) {
var projectSelect = $('#ProjectIds');
var options = '';
for (var i = 0; i < data.length; i++) {
options += optionTemplate.replace('[VALUE]', data[i].id).replace('[TEXT]', data[i].name);
}
projectSelect.empty().append(options).removeAttr('disabled').trigger('chosen:updated');
}
});
});
$('#CustomerId').trigger('change');
});
</script>
#using (Html.BeginForm("GenerateCopyReportExcel", "Report", FormMethod.Post))
{
//todo - Fix projectids validation
<div id="DlgStockReportSearch" class="modal fade" data-backdrop="static" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3>#Language.KPIReport</h3>
</div>
<div class="modal-body clearfix">
<div class="col-xs-12">
<div class="form-group">
<label for="CustomerId">#Language.Customer</label>
#Html.DropDownListFor(x => x.CustomerId, customerDropdown, new { #class = "form-control chosen", placeholder = Language.Customer })
#Html.ValidationMessageFor(x => x.CustomerId)
</div>
<div class="form-group required">
<label for="DateRange">#Language.Date</label>
#Html.EditorFor(x => x.DateRange)
</div>
<div class="form-group">
<label for="ProjectIds">#Language.Projects</label>
<div class="input-group button">
#Html.ListBoxFor(x => x.ProjectIds, projectDropdown, new { #class = "form-control chosen", multiple = ""})
#Html.ValidationMessageFor(m => m.ProjectIds)
<span class="input-group-addon">
<button type="button" class="btn btn-default chosen-select-all">Select all</button>
</span>
</div>
</div>
<div class="form-group">
<label for="LocationsIds">#Language.Location</label>
<div class="input-group button">
#Html.ListBoxFor(x => x.LocationsIds, locationDropdown, new { #class = "form-control chosen", multiple = "" })
<span class="input-group-addon">
<button type="button" class="btn btn-default chosen-select-all">Select all</button>
</span>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal" aria-hidden="true">#Language.Close</button>
<button type="submit" class="btn btn-primary">
#Language.GenerateReport
</button>
</div>
</div>
</div>
</div>
}
For some reason, I am able to submit the form even if i leave the listboxfor for projects and locations empty.

Related

ajax.beginform() not working asp.net .netFramework 4.7.2

i have trouble with my code, ajax beginForm does not make reference with the controller event, i have all the library.
the view :
#using MyWebsite.Models
#model UsersCLS
#{
ViewBag.Title = "Index";
}
<h1 style="text-align:center;">My Web Site</h1>
<section class="vh-100">
<div class="container py-5 h-100">
<div class="row d-flex align-items-center justify-content-center h-100">
<div class="col-md-8 col-lg-7 col-xl-6">
<img src="https://mdbcdn.b-cdn.net/img/Photos/new-templates/bootstrap-login-form/draw2.svg" class="img-fluid" alt="Phone image">
</div>
<div class="col-md-7 col-lg-5 col-xl-5 offset-xl-1">
<form>
<!-- Email input -->
<div class="form-outline mb-4">
<input type="email" id="form1Example13" class="form-control form-control-lg" />
<label class="form-label" for="form1Example13">Email address</label>
</div>
<!-- Password input -->
<div class="form-outline mb-4">
<input type="password" id="form1Example23" class="form-control form-control-lg" />
<label class="form-label" for="form1Example23">Password</label>
</div>
<div>
<!-- Checkbox -->
<div class="form-check">
<input class="form-check-input"
type="checkbox"
value=""
id="form1Example3"
checked />
<label class="form-check-label" for="form1Example3"> Remember me </label>
</div>
</div>
<div>
Forgot password?
</div>
<!-- Submit button -->
<button type="submit" class="btn btn-primary btn-lg btn-block">Sign in</button>
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary btn-lg btn-block" data-bs-toggle="modal" data-bs-target="#exampleModal">
Check in
</button>
<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
</div>
#using (Ajax.BeginForm("Add", "Login", null, new AjaxOptions
{
HttpMethod = "POST",
OnSuccess = "Create",
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "error"
}))
{
<div>
#Html.Label("Name")
#Html.TextBox("name", null, new { #class = "form-control" })
</div>
<div>
#Html.Label("LastName")
#Html.TextBox("lastName", null, new { #class = "form-control" })
</div>
<div>
#Html.Label("Email")
#Html.TextBox("email", null, new { #class = "form-control", #type = "email" })
</div>
<div>
#Html.Label("UserName")
#Html.TextBox("userName", null, new { #class = "form-control" })
</div>
<div>
#Html.Label("Password")
#Html.Password("password", null, new { #class = "form-control" })
</div>
<button type="button" id="btnClose" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Save changes </button>
}
<div id="error">
</div>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</section>
<script src="~/Scripts/jquery-3.6.0.min.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>
<script src="~/Scripts/bootstrap.js"></script>
this is the Model:
public class UsersCLS
{
[Display(Name ="Id")]
public int id { get; set; }
[Display(Name = "User")]
public string userName { get; set; }
[Display(Name = "Password")]
public string password { get; set; }
[Display(Name = "Name")]
public string name { get; set; }
[Display(Name = "Last Name")]
public string lastName { get; set; }
[Display(Name = "Email")]
public string email { get; set; }
}
And the Controller :
public ActionResult Index()
{
return View();
}
public string Add(UsersCLS usersCLS)
{
string answer = "";
if (!ModelState.IsValid)
{
}
else
{
using (var bd = new MyWebsiteEntities())
{
int count = 0;
using (var transc = new TransactionScope())
{
count = bd.Users.Where(p => p.UserName.Equals(usersCLS.userName)).Count();
if (count > 0)
{
answer += "<ul class='list-group'>";
answer += "<li></li>";
answer += "</ul>";
}
else
{
Users users = new Users();
users.UserName = usersCLS.userName;
SHA256Managed sha = new SHA256Managed();
byte[] bypass = Encoding.Default.GetBytes(usersCLS.password);
byte[] bycryptoPass = sha.ComputeHash(bypass);
string cryptoPass = BitConverter.ToString(bycryptoPass).Replace("-", "");
users.Password = cryptoPass;
users.Name = usersCLS.name;
users.UserName = usersCLS.userName;
users.LastName = usersCLS.lastName;
users.Email = usersCLS.email;
users.Enabled = true;
bd.Users.Add(users);
answer = bd.SaveChanges().ToString();
if (answer == "0")
{
answer = "";
}
transc.Complete();
}
}
}
}
return answer;
}
}
But (Ajax.BeginForm("Add", "Login")
does not make reference with controller event Please help, sorry for my bad english have a nice Day and thank you.

Unable to pass RadioButton group selected value to Controller

I have a ViewModel defined as:
public class Fixtures
{
public int Id { get; set; }
public string HomeTeam { get; set; }
public string AwayTeam { get; set; }
public string HomeTeamCode { get; set; }
public string AwayTeamCode { get; set; }
public string League { get; set; }
public bool Home { get; set; }
public bool Draw { get; set; }
public bool Away { get; set; }
public string FixturePrediction { get; set; }
}
My view displays a list of these fixtures and for each fixture, I have a radio button grouping. I need some way of passing the selected radio button for each fixture to my Post method. My View and controller are as below:
View:
<div class="container">
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
for (int i = 0; i < Model.Count(); i++)
{
#Html.HiddenFor(model => model[i].Id)
<div class="col-sm-6">
<div class="panel panel-primary panel-fixture">
<div class="panel-heading text-center fixturetitle">
#Html.DisplayFor(model => model[i].HomeTeam) v #Html.DisplayFor(model => model[i].AwayTeam)
#Html.HiddenFor(model => model[i].HomeTeam)
#Html.HiddenFor(model => model[i].AwayTeam)
</div>
<div class="panel-body">
<div class="container-fluid">
<div class="row">
<div class="col-xs-4 text-center">
<img src="~/Content/Images/#string.Concat(Model[i].HomeTeamCode, ".png")" class="fixturecrest" />
<div class="text-center">
#Html.DisplayFor(model => model[i].HomeTeamCode)
#Html.HiddenFor(model => model[i].HomeTeamCode)
</div>
</div>
<div class="col-xs-4 text-center">
<label class="text-center">
V <br />
</label>
</div>
<div class="col-xs-4 text-center">
<img src="~/Content/Images/#string.Concat(Model[i].AwayTeamCode, ".png")" class="fixturecrest" />
<div class="text-center">
#Html.DisplayFor(model => model[i].AwayTeamCode)
#Html.HiddenFor(model => model[i].AwayTeamCode)
</div>
</div>
</div>
<hr />
<div class="row">
<div class="text-center" role="group" aria-label="Fixture Prediction">
<div class="col-xs-4 text-center">
<button type="button" class="btn btn-xs btn-default">
HOME
#Html.RadioButtonFor(model => model[i].FixturePrediction, Model[i].Home, new { #Name = Model[i].Id })
#Html.HiddenFor(model => model[i].FixturePrediction)
<span class="glyphicon glyphicon-check"></span>
</button>
</div>
<div class="col-xs-4 text-center">
<button type="button" class="btn btn-xs btn-default">
DRAW
#Html.RadioButtonFor(model => model[i].FixturePrediction, Model[i].Draw, new { #Name = Model[i].Id })
#Html.HiddenFor(model => model[i].FixturePrediction)
<span class="glyphicon glyphicon-check"></span>
</button>
</div>
<div class="col-xs-4 text-center">
<button type="button" class="btn btn-xs btn-default">
AWAY
#Html.RadioButtonFor(model => model[i].FixturePrediction, Model[i].Away, new { #Name = Model[i].Id })
#Html.HiddenFor(model => model[i].FixturePrediction)
<span class="glyphicon glyphicon-check"></span>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
}
<input type="submit" value="Confirm Predictions" class="btn btn-success" />
}
</div>
Post Method in Controller:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Test(IEnumerable<Fixtures> test)
{
if (ModelState.IsValid)
{
}
return RedirectToAction("Test");
}
My field FixturePrediction is always null and I have attempted several variations of the 'radiobuttonfor' shown above.
All I need is to assign a value to FixturePrediction based on which radiobutton is selected. What am I missing?
Thanks in advance.
I think what you are trying to do is this, I am not sure why you are doing it the way you are doing it, but this is how you would implement Html.RadioButtonFor, it takes the model prop, and the value, you can add as many buttons as you want, and the selected radio value will be passed as the value for your model prop
#Html.RadioButtonFor(model => model[i].FixturePrediction, "Home")
#Html.RadioButtonFor(model => model[i].FixturePrediction, "Draw")
#Html.RadioButtonFor(model => model[i].FixturePrediction, "Away")
Another example

Order Form in MVC - filling out same form for multiple people and saving them as a single order

So I am working on small web application where a manager can order badges for their team. I have the form and logic of saving a single form to the database done, but I need the ability to save multiple badges to a single order using the same form, then saving that single order of multiple badges to the database as a single order that can be reviewed later. Basically, I need to fill out the form for Bob, than maybe press a "add another person" button and fill one out for Jane who could have completely different info for the badge.
Here is the View:
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
#Html.ValidationSummary(true)
<div class="form-horizontal">
<div class=" page-header">
<div class=" btn-toolbar pull-right">
<div class=" btn-group">
#if (new string[] { "Bogey#email.com", "user2", "user3" }.Contains(this.User.Identity.Name))
{
<button type="button" class=" eButton">Edit Form</button>
}
</div>
</div>
<h2>Order Form</h2>
<div class=" form-group">
<div class='form-row'>
<div class='form-group col-md-6'>
<label for="user_title">Date</label>
#Html.EditorFor(model => model.Date, "{0:yyyy-MM-dd}", new { htmlAttributes = new { #class = "form-control", #type = "date" } })
#Html.ValidationMessageFor(model => model.Date, "", new { #class = "text-danger" })
</div>
</div>
<div class='form-group col-md-6'>
<label for="user_firstname">Number of Badges</label>
#Html.EditorFor(model => model.NumBadge, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.NumBadge, "", new { #class = "text-danger" })
</div>
</div>
</div>
</div>
<div class=" form-group row">
<label for="smFormGroupInput" class="col-sm-2 col-form-label col-form-label-sm">Account Number:</label>
<div class="col-sm-10">
<div class="input-group">
#Html.EditorFor(model => model.AccountNbr, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.AccountNbr, "", new { #class = "text-danger" })
<span class="input-group-btn">
#if (new string[] { "Bogey#email.com", "user2", "user3" }.Contains(this.User.Identity.Name))
{
<button class="btn btn-default" type="button">Edit Field</button>
}
</span>
</div>
</div>
</div>
<div class=" form-group row">
<label for="smFormGroupInput" class="col-sm-2 col-form-label col-form-label-sm">Account Name 1:</label>
<div class="col-sm-10">
<div class="input-group">
#Html.EditorFor(model => model.AccountName1, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.AccountName1, "", new { #class = "text-danger" })
<span class="input-group-btn">
#if (new string[] { "Bogey#email.com", "user2", "user3" }.Contains(this.User.Identity.Name))
{
<button class="btn btn-default" type="button">Edit Field</button>
}
</span>
</div>
</div>
</div>
<div class=" form-group row">
<label for="smFormGroupInput" class="col-sm-2 col-form-label col-form-label-sm">Account Name 2:</label>
<div class="col-sm-10">
<div class="input-group">
#Html.EditorFor(model => model.AccountName2, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.AccountName2, "", new { #class = "text-danger" })
<span class="input-group-btn">
#if (new string[] { "Bogey#email.com", "user2", "user3" }.Contains(this.User.Identity.Name))
{
<button class="btn btn-default" type="button">Edit Field</button>
}
</span>
</div>
</div>
</div>
<div class=" form-group row">
<label for="smFormGroupInput" class="col-sm-2 col-form-label col-form-label-sm">First Name:</label>
<div class="col-sm-10">
<div class="input-group">
#Html.EditorFor(model => model.Fname, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Fname, "", new { #class = "text-danger" })
<span class="input-group-btn">
#if (new string[] { "Bogey#email.com", "user2", "user3" }.Contains(this.User.Identity.Name))
{
<button class="btn btn-default" type="button">Edit Field</button>
}
</span>
</div>
</div>
</div>
<div class=" form-group row">
<label for="smFormGroupInput" class="col-sm-2 col-form-label col-form-label-sm">Last Name:</label>
<div class="col-sm-10">
<div class="input-group">
#Html.EditorFor(model => model.Lname, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Lname, "", new { #class = "text-danger" })
<span class="input-group-btn">
#if (new string[] { "Bogey#email.com", "user2", "user3" }.Contains(this.User.Identity.Name))
{
<button class="btn btn-default" type="button">Edit Field</button>
}
</span>
</div>
</div>
</div>
<div class=" form-group row">
<label for="smFormGroupInput" class="col-sm-2 col-form-label col-form-label-sm">ID Number:</label>
<div class="col-sm-10">
<div class="input-group">
#Html.EditorFor(model => model.IDNbr, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.IDNbr, "", new { #class = "text-danger" })
<span class="input-group-btn">
#if (new string[] { "Bogey#email.com", "user2", "user3" }.Contains(this.User.Identity.Name))
{
<button class="btn btn-default" type="button">Edit Field</button>
}
</span>
</div>
</div>
</div>
<div class=" form-group row">
<label for="smFormGroupInput" class="col-sm-2 col-form-label col-form-label-sm">Holder Type:</label>
<div class="col-sm-10">
<div class="input-group">
#Html.EditorFor(model => model.HolderType, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.HolderType, "", new { #class = "text-danger" })
<span class="input-group-btn">
#if (new string[] { "Bogey#email.com", "user2", "user3" }.Contains(this.User.Identity.Name))
{
<button class="btn btn-default" type="button">Edit Field</button>
}
</span>
</div>
</div>
</div>
<div class=" form-group row">
<label for="smFormGroupInput" class="col-sm-2 col-form-label col-form-label-sm">Neutron:</label>
<div class="col-sm-10">
<div class="input-group">
#Html.EditorFor(model => model.Neutron, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Neutron, "", new { #class = "text-danger" })
<span class="input-group-btn">
#if (new string[] { "Bogey#email.com", "user2", "user3" }.Contains(this.User.Identity.Name))
{
<button class="btn btn-default" type="button">Edit Field</button>
}
</span>
</div>
</div>
</div>
<div class=" form-group row">
<label for="smFormGroupInput" class="col-sm-2 col-form-label col-form-label-sm">Wear Location:</label>
<div class="col-sm-10">
<div class="input-group">
#Html.EditorFor(model => model.WLocation, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.WLocation, "", new { #class = "text-danger" })
<span class="input-group-btn">
#if (new string[] { "Bogey#email.com", "user2", "user3" }.Contains(this.User.Identity.Name))
{
<button class="btn btn-default" type="button">Edit Field</button>
}
</span>
</div>
</div>
</div>
<div class=" form-group row">
<label for="smFormGroupInput" class="col-sm-2 col-form-label col-form-label-sm">Use Period Description:</label>
<div class="col-sm-10">
<div class="input-group">
#Html.EditorFor(model => model.UPD, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.UPD, "", new { #class = "text-danger" })
<span class="input-group-btn">
#if (new string[] { "Bogey#email.com", "user2", "user3" }.Contains(this.User.Identity.Name))
{
<button class="btn btn-default" type="button">Edit Field</button>
}
</span>
</div>
</div>
</div>
<div class=" form-group row">
<label for="smFormGroupInput" class="col-sm-2 col-form-label col-form-label-sm">Series Name:</label>
<div class="col-sm-10">
<div class="input-group">
#Html.EditorFor(model => model.Sname, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Sname, "", new { #class = "text-danger" })
<span class="input-group-btn">
#if (new string[] { "Bogey#email.com", "user2", "user3" }.Contains(this.User.Identity.Name))
{
<button class="btn btn-default" type="button">Edit Field</button>
}
</span>
</div>
</div>
</div>
<div class=" form-group row">
<label for="smFormGroupInput" class="col-sm-2 col-form-label col-form-label-sm">Clip Type:</label>
<div class="col-sm-10">
<div class="input-group">
#Html.DropDownList("ClipType", new List<SelectListItem>
{
new SelectListItem {Text = "SL - Slotted Clip", Value = "SL"},
new SelectListItem {Text = "AL - Alligator Clip", Value = "AL"}
})
<span class="input-group-btn">
#if (new string[] { "Bogey#email.com", "user2", "user3" }.Contains(this.User.Identity.Name))
{
<button class="btn btn-default" type="button">Edit Field</button>
}
</span>
</div>
</div>
</div>
<div class=" form-group row">
<label for="smFormGroupInput" class="col-sm-2 col-form-label col-form-label-sm">Series Color:</label>
<div class="col-sm-10">
<div class="input-group">
#Html.DropDownList("SeriesColor", new List<SelectListItem>
{
new SelectListItem {Text = "GR- Green", Value = "GR"},
new SelectListItem {Text = "BL - Blue", Value = "BL"},
new SelectListItem {Text = "LB - Light Blue", Value = "LB"},
new SelectListItem {Text = "NO - None", Value = "NO"},
new SelectListItem {Text = "OR - Orange", Value = "OR"},
new SelectListItem {Text = "PP - Purple", Value = "PP"},
new SelectListItem {Text = "RD - Red", Value = "RD"}
})
<span class="input-group-btn">
#if (new string[] { "Bogey#email.com", "user2", "user3" }.Contains(this.User.Identity.Name))
{
<button class="btn btn-default" type="button">Edit Field</button>
}
</span>
</div>
</div>
</div>
<div class=" form-group row">
<label for="smFormGroupInput" class="col-sm-2 col-form-label col-form-label-sm">Frequency Color:</label>
<div class="col-sm-10">
<div class="input-group">
#Html.DropDownList("FreqColor", new List<SelectListItem>
{
new SelectListItem {Text = "GR- Green", Value = "GR"},
new SelectListItem {Text = "BL - Blue", Value = "BL"},
new SelectListItem {Text = "LB - Light Blue", Value = "LB"},
new SelectListItem {Text = "NO - None", Value = "NO"},
new SelectListItem {Text = "OR - Orange", Value = "OR"},
new SelectListItem {Text = "PP - Purple", Value = "PP"},
new SelectListItem {Text = "RD - Red", Value = "RD"}
})
<span class="input-group-btn">
#if (new string[] { "Bogey#email.com", "user2", "user3" }.Contains(this.User.Identity.Name))
{
<button class="btn btn-default" type="button">Edit Field</button>
}
</span>
</div>
</div>
</div>
<div class=" form-group row">
<label for="smFormGroupInput" class="col-sm-2 col-form-label col-form-label-sm">Badge Use:</label>
<div class="col-sm-10">
<div class="input-group">
#Html.DropDownList("BadgeUse", new List<SelectListItem>
{
new SelectListItem {Text = "PA - Participant", Value = "PA"},
new SelectListItem {Text = "AR - Area Monitor", Value = "AR"},
new SelectListItem {Text = "CO - Control", Value = "CO"}
})
<span class="input-group-btn">
#if (new string[] { "Bogey#email.com", "user2", "user3" }.Contains(this.User.Identity.Name))
{
<button class="btn btn-default" type="button">Edit Field</button>
}
</span>
</div>
</div>
</div>
if (User.Identity.Name != "Bogey#email.com")
{
<div class="form-group row">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Submit" class="btn btn-default" />
<input type="submit" value="Draft" class="btn btn-default" />
</div>
</div>
}
Here is the Model:
public class DraftsModel
{
[Key]
public int orderId { get; set; }
[Display(Name = "Date")]
public DateTime Date { get; set; }
[Display(Name = "Number of Badges")]
public string NumBadge { get; set; }
[StringLength(6, ErrorMessage = "Cannot be longer than 6 characters.")]
public string AccountNbr { get; set; }
[StringLength(17, ErrorMessage = "Cannot be longer than 17 characters.")]
public string AccountName1 { get; set; }
[StringLength(17, ErrorMessage = "Cannot be longer than 17 characters.")]
public string AccountName2 { get; set; }
[StringLength(17, ErrorMessage = "Cannot be longer than 17 characters.")]
public string Fname { get; set; }
[StringLength(17, ErrorMessage = "Cannot be longer than 17 characters.")]
public string Lname { get; set; }
[StringLength(8, ErrorMessage = "Cannot be longer than 8 characters.")]
public string IDNbr { get; set; }
[StringLength(2, ErrorMessage = "Cannot be longer than 2 characters.")]
public string HolderType { get; set; }
[StringLength(2, ErrorMessage = "Cannot be longer than 2 characters.")]
public string Neutron { get; set; }
public string BarCode { get; set; }
public string WLocation { get; set; }
[Required(ErrorMessage = "This is a required field")]
public string UPD { get; set; }
[Required(ErrorMessage = "This is a required field")]
public string Sname { get; set; }
public string CustomerKey { get; set; }
[Required(ErrorMessage = "This is a required field")]
public string ClipType { get; set; }
[Required(ErrorMessage = "This is a required field")]
public string SeriesColor { get; set; }
[Required(ErrorMessage = "This is a required field")]
public string FreqColor { get; set; }
[Required(ErrorMessage = "This is a required field")]
public string BadgeUse { get; set; }
}
public class DraftDBContext : DbContext
{
public DraftDBContext() : base("OrderForm")
{
Database.SetInitializer<DraftDBContext>(new DropCreateDatabaseIfModelChanges<DraftDBContext>());
}
public DbSet<DraftsModel> Drafts { get; set; }
}
}
And finally here is the controller:
public ActionResult Create()
{
var model = new OrderModel();
model.Date = DateTime.Today;
return View(model);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "orderId,Date,NumBadge,AccountNbr,AccountName1,AccountName2,Fname,Lname,IDNbr,HolderType,Neutron,WLocation,UPD,Sname,ClipType,SeriesColor,FreqColor,BadgeUse")] ParentModel orderModel)
{
if (ModelState.IsValid)
{
try
{
db.Orders.Add(orderModel.OrderModel);
db.SaveChanges();
}
catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
{
Exception raise = dbEx;
foreach (var validationErrors in dbEx.EntityValidationErrors)
{
foreach (var validationError in validationErrors.ValidationErrors)
{
string message = string.Format("{0}:{1}",
validationErrors.Entry.Entity.ToString(),
validationError.ErrorMessage);
// raise a new exception nesting
// the current instance as InnerException
raise = new InvalidOperationException(message, raise);
}
}
throw raise;
}
return RedirectToAction("Index");
}
return View(orderModel);
}
I know this is a pretty huge post, but I've put a lot of time into this and this is the last little thing I need to finish! Thanks if anyone can help.

Dynamically added elements validation error

I've put some Regex expression on the properties of my model that only accept numbers.
public class MaterialsViewModel
{
[Display(Name = "Material")]
public string MaterialName { get; set; }
[Range(typeof(int), "0", "999")]
[RegularExpression(#"^\d+$", ErrorMessage = "Please enter proper value")]
public int? Quantity { get; set; }
[RegularExpression(#"^\d+$", ErrorMessage = "Please enter proper value")]
public double? Cost { get; set; }
public IEnumerable<SelectListItem> CategoryList { get; set; }
public int SelectedCategory { get; set; }
public string SelectedCategoryName { get; set; }
}
I have my View in which I can add controls dynamically
Dynamically add ScopeOfWork and Materials<br />
<div id="scopes">
<h3>Scopes</h3>
Add Scope of Work
#for (int i = 0; i < Model.ScopeOfWork.Count; i++)
{
<div class="scope">
<div class="form-group">
#Html.LabelFor(m => m.ScopeOfWork[i].ScopeOfWorkName, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.TextBoxFor(m => m.ScopeOfWork[i].ScopeOfWorkName, new { #class = "form-control" })
#Html.ValidationMessageFor(m => m.ScopeOfWork[i].ScopeOfWorkName)
</div>
</div>
<input type="hidden" class="scopeindex" name="ScopeOfWork.Index" value="#i" />
<div class="indent materials">
<h4>Material</h4>
Add Material
#for (int j = 0; j < Model.ScopeOfWork[i].Materials.Count; j++)
{
<div class="material">
<div class="form-group">
#Html.LabelFor(m => m.ScopeOfWork[i].Materials[j].MaterialName, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-2">
#Html.TextBoxFor(m => m.ScopeOfWork[i].Materials[j].MaterialName, new { #class = "form-control" })
</div>
<div class="col-md-10 col-md-offset-2">
#Html.ValidationMessageFor(m => m.ScopeOfWork[i].Materials[j].MaterialName)
</div>
</div>
<div class="form-group">
#Html.LabelFor(m => m.ScopeOfWork[i].Materials[j].Quantity, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-1">
#Html.TextBoxFor(m => m.ScopeOfWork[i].Materials[j].Quantity, new { #class = "form-control" })
</div>
<div class="col-md-10 col-md-offset-2">
#Html.ValidationMessageFor(m => m.ScopeOfWork[i].Materials[j].Quantity)
</div>
</div>
<div class="form-group">
#Html.LabelFor(m => m.ScopeOfWork[i].Materials[j].Cost, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-1">
#Html.TextBoxFor(m => m.ScopeOfWork[i].Materials[j].Cost, new { #class = "form-control" })
</div>
<div class="col-md-10 col-md-offset-2">
#Html.ValidationMessageFor(m => m.ScopeOfWork[i].Materials[j].Cost)
</div>
</div>
<div class="form-group">
<label class="control-label col-md-2">Category</label>
<div class="col-xs-2">
#Html.DropDownListFor(m => m.ScopeOfWork[i].Materials[j].SelectedCategory, Model.ScopeOfWork[i].Materials[j].CategoryList, "Please select", htmlAttributes: new { #class = "form-control" })
</div>
</div>
<input type="hidden" class="materialindex" name="ScopeOfWork[#i].Materials.Index" value="#j" />
</div>
}
</div>
</div>
}
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-success" />
</div>
</div>
</div>
}
<div id="newScope" style="display:none">
<div class="scope">
<div class="form-group">
<label for="_#__ScopeOfWorkName" class="control-label col-md-2">Scope Of Work</label>
<div class="col-md-10">
<input class="form-control" type="text" id="_#__ScopeOfWorkName" name="ScopeOfWork[#].ScopeOfWorkName" value="">
<span class="field-validation-valid text-danger" data-valmsg-for="ScopeOfWork[#].ScopeOfWorkName" data-valmsg-replace="true"></span>
</div>
</div>
<input type="hidden" class="scopeindex" name="ScopeOfWork.Index" value="#" />
<div class="materials">
<h4>Material</h4>
Add Material
</div>
<hr />
</div>
</div>
<div id="newMaterial" style="display:none">
<div class="form-group">
<label for="_#__Materials_%__MaterialName" class="control-label col-md-2">Material</label>
<div class="col-md-2">
<input class="form-control" type="text" id="_#__Materials_%__MaterialName" name="ScopeOfWork[#].Materials[%].MaterialName" value="">
<span class="field-validation-valid text-danger" data-valmsg-for="ScopeOfWork[#].Materials[%].MaterialName" data-valmsg-replace="true"></span>
</div>
</div>
<div class="form-group">
<label for="_#__Materials_%__Quantity" class="control-label col-md-2">Quantity</label>
<div class="col-md-1">
<input class="form-control" type="text" id="_#__Materials_%__Quantity" name="ScopeOfWork[#].Materials[%].Quantity" value="">
<span class="field-validation-valid text-danger" data-valmsg-for="ScopeOfWork[#].Materials[%].Quantity" data-valmsg-replace="true"></span>
</div>
</div>
<div class="form-group">
<label for="_#__Materials_%__Cost" class="control-label col-md-2">Cost</label>
<div class="col-md-1">
<input class="form-control" type="text" id="_#__Materials_%__Cost" name="ScopeOfWork[#].Materials[%].Cost" value="">
<span class="field-validation-valid text-danger" data-valmsg-for="ScopeOfWork[#].Materials[%].Cost" data-valmsg-replace="true"></span>
</div>
</div>
#*Drop down*#
<div class="form-group">
<label for="_#__Materials_%__SelectedCategory" class="control-label col-md-2">Category</label>
<div class="col-xs-2">
<select class="form-control category" id="_#__Materials_%__SelectedCategory" name="ScopeOfWork[#].Materials[%].SelectedCategory">
<option value="">--Select--</option>
</select>
</div>
</div>
<input type="hidden" class="materialindex" name="ScopeOfWork[#].Materials.Index" value="%" />
</div>
<script>
var form = $('form');
var scope = $('#newScope');
var material = $('#newMaterial');
var categories = #Html.Raw(Json.Encode(Model.CategoryList));
form.on('click', '.addmaterial', function () {
var clone = material.clone();
var scopeIndex = $(this).closest('.scope').find('.scopeindex').val();
clone.html($(clone).html().replace(/#/g, scopeIndex));
var materialIndex = new Date().getTime();
clone.html($(clone).html().replace(/%/g, materialIndex));
// drop down list
var select = clone.find('.category');
$.each(categories, function(index, item) {
select.append($('<option></option>').val(item.Value).text(item.Text));
});
$(this).closest('.materials').append(clone.html());
form.data('validator', null);
$.validator.unobtrusive.parse(form);
});
$('#addScope').click(function () {
var clone = scope.clone();
var scopeIndex = new Date().getTime();
clone.html($(clone).html().replace(/#/g, scopeIndex));
$('#scopes').append(clone.html());
form.data('validator', null);
$.validator.unobtrusive.parse(form);
});
</script>
Controller:
[HttpPost]
public ActionResult _CreateProject(ProjectViewModel project)
{
tblProject projectModel = new tblProject();
tblScopeOfWork scopeModel = new tblScopeOfWork();
tblMaterial materialModel = new tblMaterial();
if (ModelState.IsValid)
{
projectModel.ProjectName = project.ProjectName;
projectModel.ProjectLocation = project.ProjectLocation;
projectModel.ProjectDescription = project.ProjectDescription;
projectModel.WorkArea = project.WorkArea;
projectModel.ModeOfPayment = project.ModeOfPayment;
projectModel.Duration = project.Duration;
projectModel.StartDate = project.StartDate;
projectModel.EndDate = project.EndDate;
projectModel.ProfitSupervision = project.ProfitSupervision;
projectModel.ProjectStatus = project.ProjectStatus;
projectModel.ForemanId = project.ForemanId;
projectModel.ClientId = project.ClientId;
db.tblProjects.Add(projectModel);
db.SaveChanges();
//Get the recently created ProjectId
var recentProjectId = db.tblProjects.OrderByDescending(x => x.ProjectId).FirstOrDefault().ProjectId;
//Get all values from List of ScopeOfWork
//Add each ScopeOfWork to the database
for (int scopeIndex = 0; scopeIndex < project.ScopeOfWork.Count; scopeIndex++)
{
scopeModel = new tblScopeOfWork();
scopeModel.ScopeOfWork = project.ScopeOfWork[scopeIndex].ScopeOfWorkName;
scopeModel.ProjectId = recentProjectId;
db.tblScopeOfWorks.Add(scopeModel);
db.SaveChanges();
//Get the recently created ScopeOfWorkId
var recentScopeOfWorkId = db.tblScopeOfWorks.OrderByDescending(x => x.ScopeOfWorkId).FirstOrDefault().ScopeOfWorkId;
//Get all materials from its corresponding ScopeOfWork and save to database
for (int materialIndex = 0; materialIndex < project.ScopeOfWork[scopeIndex].Materials.Count; materialIndex++)
{
materialModel = new tblMaterial();
materialModel.Description = project.ScopeOfWork[scopeIndex].Materials[materialIndex].MaterialName;
materialModel.Quantity = project.ScopeOfWork[scopeIndex].Materials[materialIndex].Quantity;
materialModel.Cost = project.ScopeOfWork[scopeIndex].Materials[materialIndex].Cost;
materialModel.ScopeOfWorkId = recentScopeOfWorkId;
materialModel.CategoryId = project.ScopeOfWork[scopeIndex].Materials[materialIndex].SelectedCategory;
db.tblMaterials.Add(materialModel);
}
}
db.SaveChanges();
}
project.ScopeOfWork = new List<ScopeOfWorkViewModel>
{
new ScopeOfWorkViewModel()
{
Materials = new List<MaterialsViewModel>
{
new MaterialsViewModel()
{
CategoryList = new SelectList(db.tblCategories, "CategoryId", "CategoryName")
}
}
}
};
return View(project);
}
}
The problem is I tried to put some string ('ss') on the textbox of quantity and cost to see if the validation will work and this appears The value 'ss' is not valid for Quantity and The value 'ss is not valid for Cost.but it should be Please enter proper value. And also whenever the validation occurs, the Add Material link cannot add another newMaterial.
I have tried and check these answer and also DotNetFiddle but still it has some errors.
You do not need your regex (its an int so it can only accept a number anyway). And ditto for the cost property (but why have you made it decimal if you don't allow fractions?).
The validation for the type will be performed first, and because its invalid, no further validation is performed.
Change the attribute to to a [Required]
[Range(typeof(int), "0", "999")]
[Required(ErrorMessage = "Please enter proper value")]
public int? Quantity { get; set; }
Refer also this question/answer for changing the default error message for an invalid value for a type.
As a side note, your dynamically added items will not give client side validation because you have not added the necessary data-val-* attributes. You need to inspect the html generated for the elements in the for loops and copy the html exactly, except replacing the collection indexers.
You also have an issue with your POST method because you set the ScopeOfWork property to a new collection, and wipe out all the data the user has entered before you return the view. The basic structure of your POST method should be
[HttpPost]
public ActionResult _CreateProject(ProjectViewModel project)
{
if (!ModelState.IsValid)
{
ConfigureViewModel(project);
return View(project);
}
// code to initialize your data models, save and redirect
}
private void ConfigureViewModel(ProjectViewModel model)
{
var categories = db.tblCategories;
model.CategoryList = new SelectList(categories , "CategoryId", "CategoryName");
foreach (var scope in model.ScopeOfWork)
{
foreach (var material in scope.Materials)
{
material.CategoryList = new SelectList(categories , "CategoryId", "CategoryName");
}
}
}
Note the ConfigureviewModel() method is also called from your GET method to populate the SelectLists

mvc No data in ActionResult method after submit

I have an Index page on which there is a section to write a project name and select from a dropdownlist a project type.
Below that I have a submit button that directs to the ActionResult method Create in the Projects controller.
Code:
[UPDATE]
index.cshtml:
#using reqcoll.ViewModels
#model myViewModel
#{
ViewBag.Title = "ReqColl - project";
}
#* First half *#
#using (Html.BeginForm("CreateProject", "Projects"))
{
#Html.AntiForgeryToken()
<div class="top-spacing col-md-12 col-lg-12 col-sm-12">
#RenderTopHalf(Model.modelProject)
</div>
}
#* Second half *#
#using (Html.BeginForm("CreateRequirement", "Projects"))
{
#Html.AntiForgeryToken()
<div class="form-group" id="pnSecondHalf">
#* Requirements list *#
<div class=" col-md-6 col-lg-6 col-sm-12">
#RenderBottomLeftHalf(Model.modelRequirement)
</div>
#* New/Edit requirements panel *#
<div class="col-md-6 col-lg-6 col-sm-12">
#RenderBottomRightHalf(Model.modelRequirement)
</div>
</div>
}
#* ================================================================================= ============= *#
#* Helpers *#
#helper RenderTopHalf(reqcoll.Models.Project project)
{
<div class=" well">
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="row">
#Html.LabelFor(model => project.projectName, htmlAttributes: new { #class = "control-label col-md-2 col-lg-2 col-sm-12" })
<div class="col-md-10 col-lg-10 col-sm-12">
#Html.TextBoxFor(model => project.projectName, htmlAttributes: new { #class = "ProjectNameInput" })
#Html.ValidationMessageFor(model => project.projectName)
</div>
</div>
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="row row-spacing">
#Html.LabelFor(model => project.projectType, htmlAttributes: new { #class = "control-label col-md-2 col-lg-2 col-sm-12" })
<div class="col-md-10 col-lg-10 col-sm-12">
#Html.DropDownListFor(model => project.projectType, new SelectList(
new List<Object>{
new { value = 0 , text = "...Select..." },
new { value = 1 , text = "Windows application" },
new { value = 2 , text = "Web application" },
new { value = 3 , text = "Device application"}
},
"value",
"text",
project.projectType), htmlAttributes: new { #class = "DropDownList" })
#Html.ValidationMessageFor(model => project.projectType)
</div>
<input type="hidden" value="" id="hdProjectID" />
</div>
<div class="row top-spacing col-md-offset-5 col-sm-offset-5">
<div id="pnCreate" class=" col-sm-4 col-md-4 col-lg-4">
<input type="submit" class="btn btn-default" value="Create" />
</div>
<div id="pnEdit" class=" col-sm-4 col-md-4 col-lg-4">
<input type="submit" class="btn btn-default" value="Edit" />
|
<input type="submit" class="btn btn-default" value="Delete" />
</div>
</div>
</div>
}
#helper RenderBottomLeftHalf(reqcoll.Models.Requirement requirement)
{
<div class=" well">
<table class="table">
<tr>
<th>
#if (Model.modelProject.Requirements != null)
{
var m = Model.modelProject;
if (m.Requirements.Count > 0)
{
#Html.DisplayNameFor(model => model.modelProject.Requirements[0].shortDesc)
}
}
else
{
<label class="label label-primary col-sm-12 col-md-6 col-lg-6">No requirements available</label>
}
</th>
<th></th>
</tr>
#if (Model.modelProject.Requirements != null)
{
var m = Model.modelProject;
if (m.Requirements.Count > 0)
{
foreach (var item in Model.modelProject.Requirements)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.shortDesc)
</td>
<td>
#* buttons here*#
#*#Html.ActionLink("E", "Edit", new { id = item.requirementID }) |
#Html.ActionLink("D", "Delete", new { id = item.requirementID })*#
</td>
</tr>
}
}
}
</table>
</div>
}
#helper RenderBottomRightHalf(reqcoll.Models.Requirement requirement)
{
<div class=" well">
#Html.ValidationSummary(true)
<div class="row">
#Html.LabelFor(model => requirement.shortDesc, htmlAttributes: new { #class = "control-label col-md-4 col-lg-4 col-sm-12" })
<div class="col-md-8 col-lg-8 col-sm-12">
#Html.TextBoxFor(model => requirement.shortDesc, htmlAttributes: new { #class = "RequirementShortDesc" })
#Html.ValidationMessageFor(model => requirement.shortDesc)
</div>
</div>
#Html.ValidationSummary(true)
<div class="row row-spacing">
#Html.LabelFor(model => requirement.longDesc, htmlAttributes: new { #class = "control-label col-md-4 col-lg-4 col-sm-12" })
<div class="col-md-8 col-lg-8 col-sm-12 RequirementLongDesc">
#Html.EditorFor(model => requirement.longDesc)
#Html.ValidationMessageFor(model => requirement.longDesc)
</div>
</div>
#Html.ValidationSummary(true)
<div class="row row-spacing">
#Html.LabelFor(model => requirement.priorityCode, htmlAttributes: new { #class = "control-label col-md-4 col-lg-4 col-sm-12" })
<div class="col-md-8 col-lg-8 col-sm-12">
#foreach (var value in Enum.GetValues(requirement.priorityCode.GetType()))
{
<div class="control-label col-sm-5 col-md-5 col-lg-5">
#Html.RadioButtonFor(m => requirement.priorityCode, value)
#Html.Label(value.ToString())
</div>
}
#Html.ValidationMessageFor(model => requirement.priorityCode)
</div>
</div>
<input type="hidden" value="" id="hdRequirementID" />
<div class="row top-spacing col-md-offset-5 col-sm-offset-5">
<div id="pnReqCreate" class=" col-sm-12 col-md-6 col-lg-6">
#* submit button here *#
#*#Html.ActionLink("Add", "Add", "Requirement", new { #class = "btn btn-default btnSize" })*#
</div>
<div id="pnReqEdit" class=" col-sm-12 col-md-6 col-lg-6">
#* submit buttons here *#
#*#Html.ActionLink("Edit", "Edit", "Requirement", new { #class = "btn btn-default btnSize" })
#Html.ActionLink("Delete", "Delete", "Requirement", new { #class = "btn btn-default btnSize" })*#
</div>
</div>
</div>
}
#section Scripts {
<script>
$(function () {
var pID = $('#hdProjectID').val();
if (pID != null) {
if (pID.length > 0) {
$('#pnEdit').show();
$('#pnCreate').hide();
$('#pnSecondHalf').show();
} else {
$('#pnEdit').hide();
$('#pnCreate').show();
$('#pnSecondHalf').hide();
}
} else {
$('#pnEdit').hide();
$('#pnCreate').show();
$('#pnSecondHalf').hide();
}
var rID = $('#hdRequirementID').val();
if (rID != null) {
if (rID.length > 0) {
$('#pnReqEdit').show();
$('#pnReqCreate').hide();
} else {
$('#pnReqEdit').hide();
$('#pnReqCreate').show();
}
} else {
$('#pnReqEdit').hide();
$('#pnReqCreate').show();
}
});
</script>
#Scripts.Render("~/bundles/jqueryval")
}
ViewModel:
using reqcoll.Models;
namespace reqcoll.ViewModels
{
public class myViewModel
{
public Project modelProject;
public Requirement modelRequirement;
}
}
Controller:
using System.Web.Mvc;
using reqcoll.Models;
using reqcoll.ViewModels;
namespace reqcoll.Controllers
{
public class ProjectsController : Controller
{
private myContext db = new myContext();
// GET: Projects
public ActionResult Index()
{
// allow more than one model to be used in the view
var vm = new myViewModel()
{
modelProject = new Project() { projectName = "test", projectType = 1 },
modelRequirement = new Requirement() { requirementID = -1 },
};
return View(vm);
}
[HttpPost]
[ValidateAntiForgeryToken]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult CreateProject(myViewModel vm)
{
if (vm != null)
{
var ab = Request.Form;
// key 1: __RequestVerificationToken
// key 2: project.projectName
// key 3: project.projectType
if (ModelState.IsValid)
{
Project project = vm.modelProject;
// db.Project.Add(project.Item1);
// db.SaveChanges();
// return RedirectToAction("Index");
}
}
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}
[ORIGINAL]
#using (Html.BeginForm("Create", "Projects"))
{
#Html.AntiForgeryToken()
<div class="top-spacing col-md-12 col-lg-12 col-sm-12">
<div class=" well">
#Html.ValidationSummary(true)
<div class="row">
#Html.LabelFor(model => model.Item1.projectName, htmlAttributes: new { #class = "control-label col-md-2 col-lg-2 col-sm-12" })
<div class="col-md-10 col-lg-10 col-sm-12">
#Html.TextBoxFor(model => model.Item1.projectName, htmlAttributes: new { #class = "ProjectNameInput" })
#Html.ValidationMessageFor(model => model.Item1.projectName)
</div>
</div>
#Html.ValidationSummary(true)
<div class="row row-spacing">
#Html.LabelFor(model => model.Item1.projectType, htmlAttributes: new { #class = "control-label col-md-2 col-lg-2 col-sm-12" })
<div class="col-md-10 col-lg-10 col-sm-12">
#Html.DropDownListFor(model => model.Item1.projectType, new SelectList(
new List<Object>{
new { value = 0 , text = "...Select..." },
new { value = 1 , text = "Windows application" },
new { value = 2 , text = "Web application" },
new { value = 3 , text = "Device application"}
},
"value",
"text",
0), htmlAttributes: new { #class = "DropDownList" })
#Html.ValidationMessageFor(model => model.Item1.projectType)
</div>
<input type="hidden" value="" id="hdProjectID" />
</div>
<div class="row top-spacing col-md-offset-5 col-sm-offset-5">
<div id="pnCreate" class=" col-sm-4 col-md-4 col-lg-4">
<input type="submit" class="btn btn-default" value="Create" />
</div>
<div id="pnEdit" class=" col-sm-4 col-md-4 col-lg-4">
<input type="submit" class="btn btn-default" value="Edit" />
|
<input type="submit" class="btn btn-default" value="Delete" />
</div>
</div>
</div>
</div>
}
ProjectsController:
private myContext db = new myContext();
// GET: Projects
public ActionResult Index()
{
// allow more than one model to be used in the view
return View(new Tuple<Project, Requirement, Priority>(new Project(), new Requirement(), new Priority()));
}
[HttpPost]
[ValidateAntiForgeryToken]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create([Bind(Include = "projectName,projectType")] Project project)
{
if (ModelState.IsValid)
{
db.Project.Add(project);
db.SaveChanges();
return RedirectToAction("Index");
}
return RedirectToAction("Index");
}
So when the submit button is clicked, the ActionResult Create is called, but the ModelState is not valid and does not have the information enterd by the user.
What am I doing wrong?
Your model is looking like complex object as you are using model.Item1.projectName and model.Item1.projectType, but in action method you are trying to get values directly which is wrong.
[Updated code]
With the new code posted, this quick correction to your model will allow it to bind correctly from your view:
namespace reqcoll.ViewModels
{
public class myViewModel
{
public Project project;
public Requirement requirement;
}
}
[Original]
Despite the fact of using a Tuple<> type instead of defining a class that would encapsulate the data to pass to the view. You can still achieve what you want by creating a helper in your view.
#helper RenderMyProject(Project project) {
...
#Html.TextBoxFor(x=> project.projectType)
...
}
Then, you will call this helper
#RenderMyProject(model.Item1)
Whats the difference?
The name of the input will change. Instead of posting [Item1.projectType] Inside the response object to your controller, it will look like [project.projectType] which will be mapped to your project parameter automatically.
Found the problem.
I added {get; set;} in the myViewModel to both the models and then it worked.
so:
using reqcoll.Models;
namespace reqcoll.ViewModels
{
public class myViewModel
{
public Project Project { get; set; }
public Requirement Requirement { get; set; }
}
}

Resources