HttpPostedFileBase value is always null in controller method - asp.net-mvc

I want to be able to upload an image using a view.
So far here's what I've done based on this blog here:
I have a model extension that contains the following item:
[FileSize(10240)]
[FileTypes("jpg,jpeg,png")]
public HttpPostedFileBase mCardImage { get; set; }
FYI: the FileSize and FileTypes attributes are shaped like this:
public class FileSizeAttribute : ValidationAttribute
{
private readonly int mMaxSize;
public FileSizeAttribute(int _maxSize)
{
mMaxSize = _maxSize;
}
public override bool IsValid(object _value)
{
if (_value == null)
{
return true;
}
return mMaxSize > (_value as HttpPostedFile).ContentLength;
}
public override string FormatErrorMessage(string _name)
{
return string.Format("The file size shoud not exceed {0}", mMaxSize);
}
}
public class FileTypesAttribute: ValidationAttribute
{
private readonly List<string> mTypes;
public FileTypesAttribute(string _types)
{
mTypes = _types.Split(',').ToList();
}
public override bool IsValid(object _value)
{
if (_value == null)
{
return true;
}
var fileExt = System.IO.Path.GetExtension((_value as HttpPostedFile).FileName).Substring(1);
return mTypes.Contains(fileExt, StringComparer.OrdinalIgnoreCase);
}
public override string FormatErrorMessage(string _name)
{
return String.Format("Invalid file type. Only the following types: {0} are supported.",
String.Join(", ", mTypes));
}
}
And my view is rendered like this:
#model MyApp.Utilities.ModelExtensions.CardInfoExtension
<h2>Create new Card</h2>
#using (Html.BeginForm())
{
#Html.ValidationSummary(true)
<div class="formStyle">
<div class="float-right baseMargin">
#Html.Image(Model.mCardImageLink, Model.mCardName, null)<br/>
#Html.TextBoxFor(_item => _item.mCardImage, new { #type = "file" } )
#Html.ValidationMessageFor(_item => _item.mCardImage)
</div>
#Html.HiddenFor(_item => _item.mCardImageLink)
<div class="baseFontSize">
#Html.LabelFor(_item => _item.mCardName)
</div>
<div class="baseFontSize">
#Html.EditorFor(_item => _item.mCardName)
#Html.ValidationMessageFor(_item => _item.mCardName)
</div>
<div class="baseFontSize">
#Html.LabelFor(_item => _item.mCardNumber)
</div>
<div class="baseFontSize">
#Html.EditorFor(_item => _item.mCardNumber)
#Html.ValidationMessageFor(_item => _item.mCardNumber)
</div>
<div class="baseFontSize">
#Html.LabelFor(_item => _item.mCardColor)
</div>
<div>
#Html.Image("~\\Images\\Functional\\blueColor.jpeg", "Blue", null) #Html.CheckBoxFor(_item => _item.mBlueColor, Model.mBlueColor)
#Html.Image("~\\Images\\Functional\\redColor.jpeg", "Blue", null) #Html.CheckBoxFor(_item => _item.mRedColor, Model.mRedColor)
#Html.Image("~\\Images\\Functional\\greenColor.jpeg", "Blue", null) #Html.CheckBoxFor(_item => _item.mGreenColor, Model.mGreenColor) <br/>
#Html.Image("~\\Images\\Functional\\blackColor.jpeg", "Blue", null) #Html.CheckBoxFor(_item => _item.mBlackColor, Model.mBlackColor)
#Html.Image("~\\Images\\Functional\\whiteColor.jpeg", "Blue", null) #Html.CheckBoxFor(_item => _item.mWhiteColor, Model.mWhiteColor)<br/>
</div>
<div class="baseFontSize">
#Html.LabelFor(_item => _item.mCardManaCost)
</div>
<div class="baseFontSize">
#Html.EditorFor(_item => _item.mCardManaCost)
#Html.ValidationMessageFor(_item => _item.mCardManaCost)
</div>
<div class="baseFontSize">
#Html.LabelFor(_item => _item.mCardManaConverted)
</div>
<div class="baseFontSize">
#Html.EditorFor(_item => _item.mCardManaConverted)
#Html.ValidationMessageFor(_item => _item.mCardManaConverted)
</div>
<div class="baseFontSize">
#Html.LabelFor(_item => _item.mCardType)
</div>
<div class="baseFontSize">
#Html.DropDownListFor(_item => _item.mCardType, Model.mCardTypeList, String.Empty)
#Html.ValidationMessageFor(_item => _item.mCardType)<br/>
<span class="baseFontSize">Additional Type</span>
#Html.TextBoxFor(_item => _item.mAdditionalCardType)
</div>
<div class="baseFontSize">
#Html.LabelFor(_item => _item.mCardPower)
</div>
<div class="baseFontSize">
#Html.DropDownListFor(_item => _item.mCardPower, Model.mCardPowerList, "--- Select a value---")
#Html.ValidationMessageFor(_item => _item.mCardPower)
</div>
<div class="baseFontSize">
#Html.LabelFor(_item => _item.mCardToughness)
</div>
<div class="baseFontSize">
#Html.DropDownListFor(_item => _item.mCardToughness, Model.mCardToughnessList, "--- Select a value---")
#Html.ValidationMessageFor(_item => _item.mCardToughness)
</div>
<div class="baseFontSize">
#Html.LabelFor(_item => _item.mCardRarity)
</div>
<div class="baseFontSize">
#Html.DropDownListFor(_item => _item.mCardRarity, Model.mCardRarityList, String.Empty)
#Html.ValidationMessageFor(_item => _item.mCardRarity)
</div>
<div class="baseFontSize">
#Html.LabelFor(_item => _item.mCardTextAbilities)
</div>
<div class="baseFontSize">
#Html.TextAreaFor(_item => _item.mCardTextAbilities, new { #class = "textAreaWide" } )
#Html.ValidationMessageFor(_item => _item.mCardTextAbilities)
</div>
<div class="baseFontSize">
#Html.LabelFor(_item => _item.mCardTextFlavor)
</div>
<div class="baseFontSize">
#Html.TextAreaFor(_item => _item.mCardTextFlavor, new { #class = "textAreaWide" } )
#Html.ValidationMessageFor(_item => _item.mCardTextFlavor)
</div>
<div class="baseFontSize">
#Html.LabelFor(_item => _item.mCardArtistName)
</div>
<div class="baseFontSize">
#Html.EditorFor(_item => _item.mCardArtistName)
#Html.ValidationMessageFor(_item => _item.mCardArtistName)
</div>
<div class="baseFontSize">
#Html.LabelFor(_item => _item.mCardSet)
</div>
<div class="baseFontSize">
#Html.DropDownListFor(_item => _item.mCardSetID, ViewBag.List as SelectList, "--- Select Card Set ---", new { #class = "CardSetInfo"} )
#Html.ValidationMessageFor(_item => _item.mCardSet)
</div>
<div id="buttonField">
<input type="submit" value="Create" onclick="needToConfirm = false"/>
</div>
</div>
}
Finally, my controller action receives the model in which the file is located:
[HttpPost]
public ActionResult CreateCard(CardInfoExtension _card)
{
// Do some code here...
}
My question is: why does the mCardImage is ALWAYS null? I've tried uploading the whole thing manually with an input:
<input type="file" name="_file">
But, once again, in my controller method, the value is null. Why? Can anybody help me out?

I was missing this line in my view:
#using (Html.BeginForm("CreateCard", "Card", FormMethod.Post, new { enctype = "multipart/form-data" }))
Seems like by default Html views are not able to upload without this line. Adding this instead of the classic Html.BeginForm() solved the case!

You need to set the name of the <input> to match the name of the model property you want it to bind to.

Related

View returns null value when using two models in MVC

I have a simple MVC program where earlier I was using one model and the code was working fine. here is the code.
View
#model Microsoft.Azure.ActiveDirectory.GraphClient.User
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Create User</title>
</head>
<body>
<h2>Create User</h2><br />
#using (Html.BeginForm("Create",
"CreateUsers",
new { id = Model != null ? Model.ObjectId : "" },
FormMethod.Post,
new { enctype = "multipart/form-data" })
)
{
<fieldset>
<legend>User</legend>
<div class="editor-label">
#Html.LabelFor(model => model.UserPrincipalName)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.UserPrincipalName)
#Html.ValidationMessageFor(model => model.UserPrincipalName)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.AccountEnabled)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.AccountEnabled)
#Html.ValidationMessageFor(model => model.AccountEnabled)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.PasswordProfile.Password)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.PasswordProfile.Password)
#Html.ValidationMessageFor(model => model.PasswordProfile.Password)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.MailNickname)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.MailNickname)
#Html.ValidationMessageFor(model => model.MailNickname)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.DisplayName)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.DisplayName)
#Html.ValidationMessageFor(model => model.DisplayName)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.GivenName)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.GivenName)
#Html.ValidationMessageFor(model => model.GivenName)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Surname)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Surname)
#Html.ValidationMessageFor(model => model.Surname)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.JobTitle)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.JobTitle)
#Html.ValidationMessageFor(model => model.JobTitle)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Department)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Department)
#Html.ValidationMessageFor(model => model.Department)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
#Html.ActionLink("Back to List", "Index")
</div>
</body>
</html>
#if (ViewBag.ErrorMessage == "AuthorizationRequired")
{
<p>You have to sign-in. Click #Html.ActionLink("here", "Create", "Users", new { reauth = true }, null) to sign-in.</p>
}
Controller
public async Task<ActionResult> Create(string blobDetails)
{
#region POPULATE USER DETAIL IN CREATEUSER FIELDS
List<string> userDetails = blobDetails.Split(',').ToList();
User user = new User();
user.UserPrincipalName = userDetails[2] + "#xxx.com";
user.GivenName = userDetails[0];
user.Surname = userDetails[1];
user.DisplayName = userDetails[0] + " " + userDetails[1];
return View(Tuple.Create(user);
#endregion
}
[HttpPost]
public async Task<ActionResult> Create([Bind(Include ="UserPrincipalName,AccountEnabled,PasswordProfile,MailNickname,DisplayName,GivenName,Surname,JobTitle,Department")] User user)
{
ActiveDirectoryClient client = null;
client = AuthenticationHelper.GetActiveDirectoryClient();
string name = user.GivenName;
string username = user.UserPrincipalName;
return RedirectToAction("Index");
}
And this works perfectly fine , I get the values of user.GivenName and user.UserPrincipalName from view and I store it into string. Now what I am doing is that I am including one more model into this and trying to get its value from view so I did something like this:
Model
public class CreateUsers
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string PhoneNumber { get; set; }
public string PIDetails { get; set; }
public string BlobDetails { get; set; }
public string UserEmail { get; set; }
}
View
#using WebAppGraphAPI.Models
#model Tuple<Microsoft.Azure.ActiveDirectory.GraphClient.User, CreateUsers>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Create User</title>
</head>
<body>
<h2>Create User</h2><br />
#using (Html.BeginForm("Create",
"CreateUsers",
new { id = Model != null ? Model.Item1.ObjectId : ""
},
FormMethod.Post,
new { enctype = "multipart/form-data" })
)
{
#Html.ValidationSummary(true)
<fieldset>
<legend>User</legend>
<div class="editor-label">
#Html.LabelFor(tuple => tuple.Item1.UserPrincipalName)
</div>
<div class="editor-field">
#Html.EditorFor(tuple => tuple.Item1.UserPrincipalName)
#Html.ValidationMessageFor(tuple => tuple.Item1.UserPrincipalName)
</div>
<div class="editor-label">
#Html.LabelFor(tuple => tuple.Item1.AccountEnabled)
</div>
<div class="editor-field">
#Html.EditorFor(tuple => tuple.Item1.AccountEnabled)
#Html.ValidationMessageFor(tuple => tuple.Item1.AccountEnabled)
</div>
<div class="editor-label">
#Html.LabelFor(tuple => tuple.Item1.PasswordProfile.Password)
</div>
<div class="editor-field">
#Html.EditorFor(tuple => tuple.Item1.PasswordProfile.Password)
#Html.ValidationMessageFor(tuple => tuple.Item1.PasswordProfile.Password)
</div>
<div class="editor-label">
#Html.LabelFor(tuple => tuple.Item1.MailNickname)
</div>
<div class="editor-field">
#Html.EditorFor(tuple => tuple.Item1.MailNickname)
#Html.ValidationMessageFor(tuple => tuple.Item1.MailNickname)
</div>
<div class="editor-label">
#Html.LabelFor(tuple => tuple.Item1.DisplayName)
</div>
<div class="editor-field">
#Html.EditorFor(tuple => tuple.Item1.DisplayName)
#Html.ValidationMessageFor(tuple => tuple.Item1.DisplayName)
</div>
<div class="editor-label">
#Html.LabelFor(tuple => tuple.Item1.GivenName)
</div>
<div class="editor-field">
#Html.EditorFor(tuple => tuple.Item1.GivenName)
#Html.ValidationMessageFor(tuple => tuple.Item1.GivenName)
</div>
<div class="editor-label">
#Html.LabelFor(tuple => tuple.Item1.Surname)
</div>
<div class="editor-field">
#Html.EditorFor(tuple => tuple.Item1.Surname)
#Html.ValidationMessageFor(tuple => tuple.Item1.Surname)
</div>
<div class="editor-label">
#Html.LabelFor(tuple => tuple.Item1.JobTitle)
</div>
<div class="editor-field">
#Html.EditorFor(tuple => tuple.Item1.JobTitle)
#Html.ValidationMessageFor(tuple => tuple.Item1.JobTitle)
</div>
<div class="editor-label">
#Html.LabelFor(tuple => tuple.Item1.Department)
</div>
<div class="editor-field">
#Html.EditorFor(tuple => tuple.Item1.Department)
#Html.ValidationMessageFor(tuple => tuple.Item1.Department)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
#Html.ActionLink("Back to List", "Index")
</div>
<div class="editor-label">
#*#Html.LabelFor(tuple => tuple.Item2.UserEmail)*#
#Html.EditorFor(tuple => tuple.Item2.UserEmail)
</div>
</body>
</html>
#if (ViewBag.ErrorMessage == "AuthorizationRequired")
{
<p>You have to sign-in. Click #Html.ActionLink("here", "Create", "Users", new { reauth = true }, null) to sign-in.</p>
}
Controller
public async Task<ActionResult> Create(string blobDetails)
{
#region POPULATE USER DETAIL IN CREATEUSER FIELDS
List<string> userDetails = blobDetails.Split(',').ToList();
User user = new User();
user.UserPrincipalName = userDetails[2] + "#gwuadmeaoutlook.onmicrosoft.com";
user.GivenName = userDetails[0];
user.Surname = userDetails[1];
user.DisplayName = userDetails[0] + " " + userDetails[1];
CreateUsers userInfo = new CreateUsers();
userInfo.UserEmail = userDetails[4];
return View(Tuple.Create(user,userInfo));
}
[HttpPost]
public async Task<ActionResult> Create([Bind(Include ="UserPrincipalName,AccountEnabled,PasswordProfile,MailNickname,DisplayName,GivenName,Surname,JobTitle,Department")] User user,[Bind(Include ="UserEmail")] CreateUsers userInfo)
{
ActiveDirectoryClient client = null;
client = AuthenticationHelper.GetActiveDirectoryClient();
string name = user.GivenName;
string username = user.UserPrincipalName;
string Email = userInfo.UserEmail
return RedirectToAction("Index");
}
When I do this I do not get values from View in my controller strings , it shows null. Can you suggest me how can I achieve this.
You could use an object which contains all of the properties you're trying to send to the client. We generally create DTOs for this specific usage. In you're case you'd have:
CreateDto
public class CreateDto
{
public Microsoft.Azure.ActiveDirectory.GraphClient.User User { get; set; }
public CreateUsers UserInfo { get; set; }
}
Then you're controller will be updated as:
public async Task<ActionResult> Create(string blobDetails)
{
#region POPULATE USER DETAIL IN CREATEUSER FIELDS
List<string> userDetails = blobDetails.Split(',').ToList();
User user = new User();
user.UserPrincipalName = userDetails[2] + "#gwuadmeaoutlook.onmicrosoft.com";
user.GivenName = userDetails[0];
user.Surname = userDetails[1];
user.DisplayName = userDetails[0] + " " + userDetails[1];
CreateUsers userInfo = new CreateUsers();
userInfo.UserEmail = userDetails[4];
var dto = new CreateDto() {
User = user,
UserInfo = userInfo
}
return View(dto);
}
[HttpPost]
public async Task<ActionResult> Create([Bind(Include ="UserPrincipalName,AccountEnabled,PasswordProfile,MailNickname,DisplayName,GivenName,Surname,JobTitle,Department")] User user,[Bind(Include ="UserEmail")] CreateUsers userInfo)
{
ActiveDirectoryClient client = null;
client = AuthenticationHelper.GetActiveDirectoryClient();
string name = user.GivenName;
string username = user.UserPrincipalName;
string Email = userInfo.UserEmail
return RedirectToAction("Index");
}
And your view will have:
#using WebAppGraphAPI.Models
#model CreateDto
Later you can reference the information with Model.user and Model.userInfo.
I know this doesn't explain why the Tuple doesn't work. I believe that is best explain by #darin-dimitrov in ado.net mvc3 tuple using in model and single views

Create #Html.RadioButtonFor (ASP.NET MVC4)

This is Task model
namespace TaskManager.Models
{
public class Task
{
private const int DefaultTaskPriority = 1;
//some other properties
[DefaultValue(DefaultTaskPriority)]
public TaskPriority Priority { get; set; }
}
public enum TaskPriority
{
[Display(Name = "Low")]
Low = 0,
[Display(Name = "Default")]
Normal = 1,
[Display(Name = "High")]
High = 2,
[Display(Name = "Extreme")]
Extreme = 3
}
}
and this is part of my view
<div class="form-group">
#Html.LabelFor(u => u.Priority)
<div class="radio">
<label>#Html.RadioButtonFor(model => model.Priority, 0) Low</label>
</div>
<div class="radio">
<label>#Html.RadioButtonFor(model => model.Priority, 1, new { Checked = "checked" }) Default</label>
</div>
<div class="radio">
<label>#Html.RadioButtonFor(model => model.Priority, 2) High</label>
</div>
<div class="radio">
<label>#Html.RadioButtonFor(model => model.Priority, 3) Extreme</label>
</div>
#Html.ValidationMessageFor(u => u.Description, null, new {#class = "error"})
</div>
but I'm not sure, is it the right way to create radio buttons? Maybe I should get label for every radio button from model (from enum TaskPriority)? I'm new in MVC so I don't know which way is better?
UPD View using values from enum TaskPriority
<div class="form-group">
#Html.LabelFor(u => u.Priority)
<div class="radio">
<label>#Html.RadioButtonFor(model => model.Priority, TaskPriority.Low) Low</label>
</div>
<div class="radio">
<label>#Html.RadioButtonFor(model => model.Priority, TaskPriority.Normal) Normal</label>
</div>
<div class="radio">
<label>#Html.RadioButtonFor(model => model.Priority, TaskPriority.High) High</label>
</div>
<div class="radio">
<label>#Html.RadioButtonFor(model => model.Priority, TaskPriority.Extreme) Extreme</label>
</div>
#Html.ValidationMessageFor(u => u.Description, null, new {#class = "error"})
</div>

Radio button and checkbox values get null on submit in asp.net MVC

Model is list here while clicking on submit button Radio button and checkbox values get null
#foreach (var item in Model)
{
#Html.HiddenFor(m => m[Countid].ActivityType)
<div class=" ">
<div class="clear">
</div>
#Html.Label("Group Name", "Group Name", new { #class = "control-label col-md-2", #style = "font-weight:700" })
<label class="control-label col-md-2"> : #item.PfpActivityGroup.ActivityGroupName</label>
</div>
<div class="">
#Html.Label("Activity Type", "Activity Type", new { #class = "control-label col-md-2", #style = "font-weight:700" })
<label class="control-label col-md-2"> : #item.ActivityType</label>
<div class="clear">
</div>
</div>
if (item.PfpQsnCreationMasters != null)
{
<div class=" ">
<label for="ActivityType" class="field-label">
Question Description
</label>
<label class="control-label col-md-2"> : #item.PfpQsnCreationMasters.SurveyDesc</label>
<div class="clear">
</div>
#Html.HiddenFor(m => m[Countid].PfpQsnCreationMasters.SurveyDesc)
#Html.DropDownList("NoOfOptions", ViewData["NoOfOptions"] as SelectList, new { #class = "hide" })
#Html.DropDownListFor(m => #item.PfpQsnCreationMasters.QuestionType, ViewData["QuestionType"] as SelectList, new { #class = "hide" })
#if (item.Type == "TextBox")
{
<div id="divTextBox">
#Html.TextBoxFor(x => item.txtQsnDesc);
#Html.HiddenFor(x => item.txtQsnDesc)
<div class="clear">
</div>
</div>
}
#if (item.Type == "RedioButton")
{
<div >
#if (item.option1 != null)
{
#Html.RadioButtonFor(x => item.RadioOptionSelected, #item.option1)
<label class="control-label col-md-2"> #item.option1</label>
<div class="clear">
</div>
}
#if (item.option2 != null)
{
#Html.RadioButtonFor(x => item.RadioOptionSelected, #item.option2)
<label class="control-label col-md-2"> #item.option2</label>
<div class="clear">
</div>
}
#if (item.option3 != null)
{
#Html.RadioButtonFor(x => item.RadioOptionSelected, #item.option3)
<label class="control-label col-md-2"> #item.option3</label>
<div class="clear">
</div>
}
#if (item.option4 != null)
{
#Html.RadioButtonFor(x => item.RadioOptionSelected, #item.option4)
<label class="control-label col-md-2"> #item.option4</label>
<div class="clear">
</div>
}
#Html.HiddenFor(x => item.RadioOptionSelected)
</div>
}
#if (item.Type == "CheckBox")
{
<div id="divCheckBox">
#if (item.ListTextBox1 != null)
{
#Html.CheckBoxFor(x => item.IsOption1Selected) <label class="control-label col-md-2"> #item.ListTextBox1</label>
#Html.HiddenFor(x => item.IsOption1Selected)
<div class="clear">
</div>
}
#if (item.ListTextBox2 != null)
{
#Html.CheckBoxFor(x => item.IsOption2Selected) <label class="control-label col-md-2"> #item.ListTextBox2</label>
#Html.HiddenFor(x => item.IsOption2Selected)
<div class="clear">
</div>
}
#if (item.ListTextBox3 != null)
{
#Html.CheckBoxFor(x => item.IsOption3Selected) <label class="control-label col-md-2"> #item.ListTextBox3</label>
#Html.HiddenFor(x => item.IsOption3Selected)
<div class="clear">
</div>
}
#if (item.ListTextBox4 != null)
{
#Html.CheckBoxFor(x => item.IsOption4Selected) <label class="control-label col-md-2"> #item.ListTextBox4</label>
#Html.HiddenFor(x => item.IsOption4Selected)
<div class="clear">
</div>
}
</div>
}
</div>
}
<div class="clear">
</div>
<br />
Countid = Countid + 1;
}
I would ordinarily add this as a comment but I lack the +50 rep
Try putting this on the Model value of the checkbox:
public class mymodel {
[DisplayFormat(ConvertEmptyStringToNull = false)]
public bool mycheckboxvalue { get; set; }
}

Getting error of "The model item passed into the dictionary is of type...."

I'm making a website using asp.net mvc 4 & EF6 where admins can add new client information. So far everything is working fine but whenever I try to save the data by pressing Add button I get this error,
The model item passed into the dictionary is of type 'MyMvc.Models.UserInfo', but this dictionary requires a model item of type 'MyMvc.Models.BrManagement'
Here are my codes,
Controller
[HttpPost]
public ActionResult ClientManager(BrManagement ClTable)
{
if (Session["AdminNAME"] != null)
{
if (ModelState.IsValid)
{
var AddClient = ClTable.AddUserInfo;
abdb.UserInfoes.Add(AddClient);
abdb.SaveChanges();
return RedirectToAction("ClientManager", new { ClPanelId = "AllCl" });
}
return View(ClTable.AddUserInfo);
}
else
{
return RedirectToAction("AdminLogin");
}
}
Model
public class BrManagement
{
public Branch Branches { get; set; }
public IEnumerable<Branch> BrCollection { get; set; }
public UserInfo AddUserInfo { get; set; }
public IEnumerable<UserInfo> UserCollection { get; set; }
}
View
#using (Html.BeginForm("ClientManager", "Home", FormMethod.Post))
{
#Html.ValidationSummary(true)
<div class="editor-label">
<strong>Client USER ID</strong>
</div>
<div class="editor-field">
#Html.TextBoxFor(a => a.AddUserInfo.UserId)
#Html.ValidationMessageFor(a => a.AddUserInfo.UserId)
</div>
<div class="editor-label">
<strong>Client Password</strong>
</div>
<div class="editor-field">
#Html.TextBoxFor(a => a.AddUserInfo.Password)
#Html.ValidationMessageFor(a => a.AddUserInfo.Password)
</div>
<div class="editor-label">
<strong>Full Name</strong>
</div>
<div class="editor-field">
#Html.TextBoxFor(a => a.AddUserInfo.Name, new { size = 30 })
#Html.ValidationMessageFor(a => a.AddUserInfo.Name)
</div>
<div class="editor-label">
<strong>Address</strong>
</div>
<div class="editor-field">
#Html.TextBoxFor(a => a.AddUserInfo.AddressLine1, new { size = 30 })
#Html.ValidationMessageFor(a => a.AddUserInfo.AddressLine1)
</div>
#Html.HiddenFor(a => a.AddUserInfo.CreatedDate, new { #Value = System.DateTime.Now })
#Html.HiddenFor(a => a.AddUserInfo.IsActive, new { #Value = "N" })
#Html.HiddenFor(a => a.AddUserInfo.IsApproved, new { #Value = "N" })
#Html.HiddenFor(a => a.AddUserInfo.IsinfoMatched, new { #Value = "N" })
#Html.HiddenFor(a => a.AddUserInfo.IsReportView, new { #Value = "N" })
#Html.HiddenFor(a => a.AddUserInfo.IsVarified, new { #Value = "N" })
<br />
<p><input type="submit" class="btn btn-info" value="Add" /></p>
}
I've used the same code for Branches & BrCollection model, both of them are working fine. UserCollection model is also working fine. Why is this happening for AddUserInfo model? I've searched a lot but couldn't find any solution similar to mine. Need this help really bad. Your help will be appreciated! Tnx.
UPDATE
View(FULL)
#model ABCoLtd.Models.BrManagement
#{
ViewBag.Title = "ClientManager";
string active = ViewBag.ClActive.ToString();
Layout = "~/Views/Shared/_ALayout.cshtml";
}
<link href="~/Content/DataTables-1.10.4/css/jquery.dataTables.min.css" rel="stylesheet" />
<body>
<script src="~/Scripts/DataTables-1.10.4/jquery.dataTables.min.js"></script>
<script>
$(document).ready(function () {
var tabpane = '#active';
$("#" + tabpane).addClass("active");
$('#BrTable').DataTable({
"aoColumns": [
{ "bSortable": true },
{ "bSortable": true },
{ "bSortable": true },
{ "bSortable": true }
]
});
});
</script>
<br /><br /><br /><br />
<div class="container well" style="min-width: 100%; padding-right: 5px;">
<h3>Client Manager</h3><hr style="border-top: 2px solid #096596;" />
<ul class="nav nav-tabs">
<li>All Clients </li>
<li>Add Clients</li>
</ul>
<div class="tab-content">
<div class="tab-pane" id="AllCl" style="padding-top: 10px; padding-left: 10px;">
<h4>Manage Clients</h4><hr style="border-top: 2px solid #096596;" />
<div class="table-responsive">
<table id="BrTable" class="table table-striped">
<thead>
<tr><th>BOID</th><th>Name</th><th>Email</th><th>Phone</th></tr>
</thead>
<tbody>
#foreach(var item in Model.UserCollection)
{
<tr>
<td>#Html.DisplayFor(modelItem => item.UserId)</td>
<td>#Html.DisplayFor(modelItem => item.Name)</td>
<td>#Html.DisplayFor(modelItem => item.Email)</td>
<td>#Html.DisplayFor(modelItem => item.Phone1)</td></tr>
}
</tbody>
</table>
</div>
</div>
<div class="tab-pane" id="AddCl" style="padding-top: 10px; padding-left: 10px;">
<h4>Add Client</h4><hr style="border-top: 2px solid #096596;" />
#using (Html.BeginForm("ClientManager", "Home", FormMethod.Post))
{
#Html.ValidationSummary(true)
<div class="editor-label">
<strong>Client BO Account No.</strong>
</div>
<div class="editor-field">
#Html.TextBoxFor(a => a.AddUserInfo.BOAccountNo, new { size = 30 })
#Html.ValidationMessageFor(a => a.AddUserInfo.BOAccountNo)
</div>
<div class="editor-label">
<strong>Client USER ID</strong>
</div>
<div class="editor-field">
#Html.TextBoxFor(a => a.AddUserInfo.UserId)
#Html.ValidationMessageFor(a => a.AddUserInfo.UserId)
</div>
<div class="editor-label">
<strong>Client Password</strong>
</div>
<div class="editor-field">
#Html.TextBoxFor(a => a.AddUserInfo.Password)
#Html.ValidationMessageFor(a => a.AddUserInfo.Password)
</div>
<div class="editor-label">
<strong>Full Name</strong>
</div>
<div class="editor-field">
#Html.TextBoxFor(a => a.AddUserInfo.Name, new { size = 30 })
#Html.ValidationMessageFor(a => a.AddUserInfo.Name)
</div>
<div class="editor-label">
<strong>Address</strong>
</div>
<div class="editor-field">
#Html.TextBoxFor(a => a.AddUserInfo.AddressLine1, new { size = 30 })
#Html.ValidationMessageFor(a => a.AddUserInfo.AddressLine1)
</div>
#Html.HiddenFor(a => a.AddUserInfo.CreatedDate, new { #Value = System.DateTime.Now })
#Html.HiddenFor(a => a.AddUserInfo.IsActive, new { #Value = "N" })
#Html.HiddenFor(a => a.AddUserInfo.IsApproved, new { #Value = "N" })
#Html.HiddenFor(a => a.AddUserInfo.IsinfoMatched, new { #Value = "N" })
#Html.HiddenFor(a => a.AddUserInfo.IsReportView, new { #Value = "N" })
#Html.HiddenFor(a => a.AddUserInfo.IsVarified, new { #Value = "N" })
#Html.HiddenFor(a => a.AddUserInfo.UserType, new { #Value = "C" })
<br />
<p><input type="submit" class="btn btn-info" value="Add" /></p>
}
</div>
</div>
</div>
</body>
Your view expect BrManagement
#model MyMvc.Models.BrManagement
But you returned ClTable.AddUserInfo which is type of UserInfo, that is why you got that error.
To fix this, change return View(ClTable.AddUserInfo); to return View(ClTable); in your controller.

Passing Model in ViewModel to controller get null (Edit Form)

This edit form was already success, then i want to add add comment to this form, to do this i'm following this article
http://www.arrangeactassert.com/when-to-use-html-renderpartial-and-html-renderaction-in-asp-net-mvc-razor-views/
i create the viewmodel named CaseInternalEditViewModel
public class CaseInternalEditViewModel
{
public CaseInternalEditViewModel()
{
caseComment = new List<CaseComment>();
}
public String caseIDComment { get; set; }
public CaseInternal caseInternal { get; set; }
public List<CaseComment> caseComment { get; set; }
}
and change model in View :
#model myCHMTest.Models.CaseInternalEditViewModel
#{
ViewBag.Title = "Edit";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Edit</h2>
<script src="#Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="#Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<div class="container">
<fieldset>
#using (Html.BeginForm())
{
#Html.ValidationSummary(true)
<legend>CaseInternal</legend>
<div class="container">
#Html.HiddenFor(model => model.caseIDComment)
#Html.HiddenFor(model => model.caseInternal.ID)
#Html.HiddenFor(model => model.caseInternal.CaseID)
#Html.HiddenFor(model => model.caseInternal.StatusID)
#* #Html.HiddenFor(model => model.caseInternal.CreatedNIK)*#
#Html.HiddenFor(model => model.caseInternal.CreatedBy)
#Html.HiddenFor(model => model.caseInternal.CreatedDt)
#Html.HiddenFor(model => model.caseInternal.SLA)
#Html.HiddenFor(model => model.caseInternal.SLAFlag)
#Html.HiddenFor(model => model.caseInternal.DatCreated)
#Html.HiddenFor(model => model.caseInternal.DayResolved)
#Html.HiddenFor(model => model.caseInternal.CategoryID)
<div class="sixteen columns">
<div class="fbbluebox">
<div class="editor-label">
#Html.LabelFor(model => model.caseInternal.CaseID)
#Html.DisplayFor(model => model.caseInternal.CaseID)
#Html.ValidationMessageFor(model => model.caseInternal.CaseID)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.caseInternal.Title)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.caseInternal.Title)
#Html.ValidationMessageFor(model => model.caseInternal.Title)
</div>
</div>
<hr />
</div>
<div class="one-third column">
<div class="editor-label">
#Html.LabelFor(model => model.caseInternal.BranchID)
</div>
<div class="editor-field">
#Html.DropDownList("BranchID")
#Html.ValidationMessageFor(model => model.caseInternal.BranchID)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.caseInternal.ProjectID, "InternalProject")
</div>
<div class="editor-field">
#Html.DropDownList("ProjectID", null, new { #onchange = "javascript:cascadingdropdown();" })
#Html.ValidationMessageFor(model => model.caseInternal.ProjectID)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.caseInternal.SubProjectID, "InternalSubProject")
</div>
<div class="editor-field">
#Html.DropDownList("SubProjectID", String.Empty)
#*#Html.DropDownListFor(model => model.SubProjectID, new SelectList(Enumerable.Empty<SelectListItem>()))*#
#Html.ValidationMessageFor(model => model.caseInternal.SubProjectID)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.caseInternal.PengaduanID, "InternalPPengaduan")
</div>
<div class="editor-field">
#Html.DropDownList("PengaduanID", String.Empty)
#Html.ValidationMessageFor(model => model.caseInternal.PengaduanID)
</div>
</div>
<div class="one-third column">
<div class="editor-label">
#Html.LabelFor(model => model.caseInternal.CreatedNIK)
</div>
<div class="editor-field">
#* #Html.TextBoxFor(model => model.caseInternal.CreatedNIK, new { disabled = "disabled" })
#Html.ValidationMessageFor(model => model.caseInternal.CreatedNIK)*#
<input type="text" name="InitialNIK" value="#Model.caseInternal.CreatedNIK" disabled="disabled"/>
</div>
<div class="editor-label">
#Html.LabelFor(model => model.caseInternal.CreatedBy)
</div>
<div class="editor-field">
#Html.TextBoxFor(model => model.caseInternal.CreatedBy, new { disabled = "disabled" })
#Html.ValidationMessageFor(model => model.caseInternal.CreatedBy)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.caseInternal.CreatedDt)
</div>
<div class="editor-field">
#Html.TextBoxFor(model => model.caseInternal.CreatedDt, new { disabled = "disabled" })
#Html.ValidationMessageFor(model => model.caseInternal.CreatedDt)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.caseInternal.AssignGroupID)
</div>
<div class="editor-field">
#Html.DropDownList("AssignGroupID", null, new { #onchange = "javascript:memberdropdown();" })
#Html.ValidationMessageFor(model => model.caseInternal.AssignGroupID)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.caseInternal.AssignMemberID)
</div>
<div class="editor-field">
#Html.DropDownList("AssignMemberID", String.Empty)
#Html.ValidationMessageFor(model => model.caseInternal.AssignMemberID)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.caseInternal.StatusID)
</div>
<div class="editor-field">
#Html.DropDownList("StatusID",null, new { #disabled = "disabled" })
#Html.ValidationMessageFor(model => model.caseInternal.StatusID)
</div>
</div>
<div class="one-third column">
<div class="editor-label">
#Html.LabelFor(model => model.caseInternal.SLA)
</div>
<div class="editor-field">
#Html.TextBoxFor(model => model.caseInternal.SLA, new { disabled = "disabled" })
#Html.ValidationMessageFor(model => model.caseInternal.SLA)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.caseInternal.SLAFlag)
</div>
<div class="editor-field">
#Html.TextBoxFor(model => model.caseInternal.SLAFlag, new { disabled = "disabled" })
#Html.ValidationMessageFor(model => model.caseInternal.SLAFlag)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.caseInternal.DayResolved)
</div>
<div class="editor-field">
#Html.TextBoxFor(model => model.caseInternal.DayResolved, new { disabled = "disabled" })
#Html.ValidationMessageFor(model => model.caseInternal.DayResolved)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.caseInternal.DatCreated)
</div>
<div class="editor-field">
#Html.TextBoxFor(model => model.caseInternal.DatCreated, new { disabled = "disabled" })
#Html.ValidationMessageFor(model => model.caseInternal.DatCreated)
</div>
</div>
<div class="sixteen columns">
<div class="fbgreybox" >
<div class="editor-label">
#Html.LabelFor(model => model.caseInternal.Description)
</div>
<div class="editor-field">
#Html.TextAreaFor(model => model.caseInternal.Description, new { style = "width: 100%; height: 100px;" })
#Html.ValidationMessageFor(model => model.caseInternal.Description)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.caseInternal.LinkCase)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.caseInternal.LinkCase)
#Html.ValidationMessageFor(model => model.caseInternal.LinkCase)
</div>
</div>
<p>
<input type="submit" value="Save" class="uibutton large confirm"/>
#Html.ActionLink("Back to List", "Index", "CaseInternal", new { #class = "uibutton" })
</p>
</div>
</div>
}
</fieldset>
</div>
#using (Ajax.BeginForm("Comment", "CaseInternal", new AjaxOptions { InsertionMode = InsertionMode.Replace, UpdateTargetId = "myGrid", OnSuccess = "done" }))
{
#*<input type="text" id="caseEka" name="caseEka" />*#
#Html.HiddenFor(model => model.caseIDComment)
<input type="text" id="Comment" name="Comment"/>
}
<div id="myGrid">
#Html.Partial("_showComment", Model.caseComment)
</div>
<script type="text/javascript">
function done() {
document.getElementById('Comment').value = '';
}
function cascadingdropdown() {
var idDept = $("#ProjectID").val();
var subProject = $('#SubProjectID').val();
var urlemp = '#Url.Action("GetSubProjectFromProjectID")';
var select = $('#SubProjectID');
$.ajax({
type: "POST",
url: urlemp,
data: { id: idDept },
error: function (jqXHR, textStatus, errorThrown) {
alert("error" + jqXHR.responseText);
},
success: function (returndata) {
if (returndata.ok) {
select.empty();
$.each(returndata.data, function (index, itemData) {
if (subProject == itemData.ID) {
select.append($('<option selected="selected"></option>').val(itemData.ID).html(itemData.SubProjectName));
} else {
select.append($('<option></option>').val(itemData.ID).html(itemData.SubProjectName));
}
});
select.show('slow');
// $("#ProjectID").attr("disabled", "disabled");
}
else {
window.alert(' error : ' + returndata.message);
}
}
}
);
}
function getNameNik() {
window.alert("AAAASSDSD");
var idDept = $("#CreatedNIK").val();
var urlemp = '#Url.Action("GetNameNik")';
$.ajax({
type: "POST",
url: urlemp,
data: { id: idDept },
error: function (jqXHR, textStatus, errorThrown) {
alert("error" + jqXHR.responseText);
},
success: function (returndata) {
if (returndata.ok) {
$('#nikLabel').text("");
$.each(returndata.data, function (index, itemData) {
$('#nikLabel').text(itemData.FirstName + " " + itemData.LastName);
});
}
else {
window.alert(' error : ' + returndata.message);
}
}
}
);
}
function memberdropdown() {
var idDept = $("#AssignGroupID").val();
var subProject = $('#AssignMemberID').val();
var urlemp = '#Url.Action("GetMemberFromGroupID")';
var select = $('#AssignMemberID');
$.ajax({
type: "POST",
url: urlemp,
data: { id: idDept },
error: function (jqXHR, textStatus, errorThrown) {
alert("error" + jqXHR.responseText);
},
success: function (returndata) {
select.empty();
if (returndata.ok) {
$.each(returndata.data, function (index, itemData) {
if (subProject == itemData.ID) {
select.append($('<option selected="selected"></option>').val(itemData.ID).html(itemData.UserName));
} else {
select.append($('<option></option>').val(itemData.ID).html(itemData.UserName));
}
});
select.show('slow');
// $("#ProjectID").attr("disabled", "disabled");
}
else {
window.alert(' error : ' + returndata.message);
}
}
}
);
}
function start() {
cascadingdropdown();
memberdropdown();
}
window.onload = start;
// window.onload = memberdropdown;
</script>
and here my controller:
public ActionResult Edit(string id)
{
CaseInternal caseinternal = db.CaseInternals.Find(id);
caseinternal.NullSafeTrimStrings();
ViewBag.PengaduanID = new SelectList(db.InternalPPengaduans, "ID", "PPengaduanName", caseinternal.PengaduanID);
ViewBag.ProjectID = new SelectList(db.InternalProjects, "ID", "ProjectName", caseinternal.ProjectID);
ViewBag.SubProjectID = new SelectList(db.InternalSubProjects, "ID", "SubProjectName", caseinternal.SubProjectID);
ViewBag.CategoryID = new SelectList(db.MasterCategories, "ID", "CategoryName", caseinternal.CategoryID);
ViewBag.AssignGroupID = new SelectList(db.MasterGroups, "ID", "GroupName", caseinternal.AssignGroupID);
ViewBag.AssignMemberID = new SelectList(db.MasterAssignUsers, "ID", "UserName", caseinternal.AssignMemberID);
ViewBag.BranchID = new SelectList(branchObject.A2BR, "BRCODE", "BRNAME", caseinternal.BranchID);
ViewBag.StatusID = new SelectList(db.MasterStatus, "ID", "StatusName", caseinternal.StatusID);
CaseInternalEditViewModel caseInternalEdit = new CaseInternalEditViewModel();
caseInternalEdit.caseInternal = caseinternal;
caseInternalEdit.caseIDComment = caseinternal.CaseID;
var commentCase = db.CaseComments.Where(p => p.CaseID == caseinternal.CaseID).OrderByDescending(p => p.CreatedDt);
foreach (CaseComment cas in commentCase)
{
caseInternalEdit.caseComment.Add(cas);
}
return View(caseInternalEdit);
}
[HttpPost]
public ActionResult Edit(CaseInternalEditViewModel caseinternalEdit)
{
//CaseInternalEditViewModel caseinternalEdit
CaseInternal caseinternal = caseinternalEdit.caseInternal;
if (ModelState.IsValid)
{
db.Entry(caseinternal).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.PengaduanID = new SelectList(db.InternalPPengaduans, "ID", "PPengaduanName", caseinternal.PengaduanID);
ViewBag.ProjectID = new SelectList(db.InternalProjects, "ID", "ProjectName", caseinternal.ProjectID);
ViewBag.SubProjectID = new SelectList(db.InternalSubProjects, "ID", "SubProjectName", caseinternal.SubProjectID);
ViewBag.CategoryID = new SelectList(db.MasterCategories, "ID", "CategoryName", caseinternal.CategoryID);
ViewBag.AssignGroupID = new SelectList(db.MasterGroups, "ID", "GroupName", caseinternal.AssignGroupID);
ViewBag.AssignMemberID = new SelectList(db.MasterAssignUsers, "ID", "UserName", caseinternal.AssignMemberID);
ViewBag.BranchID = new SelectList(branchObject.A2BR, "BRCODE", "BRNAME", caseinternal.BranchID);
ViewBag.StatusID = new SelectList(db.MasterStatus, "ID", "StatusName", caseinternal.StatusID);
return View(caseinternalEdit);
}
the modelstate is always invalid, because some field is null, field2 in dropdownlist is null when passing to controller.
is the viewbag the problem? should i change the name of viewbag?
#Html.EditorFor(model => model.caseInternal.Title)
is rendered to
<input class="text-box single-line" id="caseInternal_Title" name="caseInternal.Title" type="text" value="ViewModel" />
but dropdownlist
<select id="BranchID" name="BranchID"><option selected="selected" value="001">KANTOR PUSAT NON OPERASIONAL </option>
the different is the name, the branch should be caseinternal.branchID maybe it will work, but how to do that?
You have:
#Html.DropDownList("BranchID")
You want:
#Html.DropDownListFor(model => model.caseInternal.BranchID)
OR if you really don't want to use a DropDownListFor() for whatever reason....
#Html.DropDownList("caseInternal_BranchID")

Resources