Post parameter is null - post

I Have controller with following methods:
public ActionResult Create()
{
return View();
}
[Authorize]
[HttpPost]
public ActionResult Create(Tests test)
{
test.CreateDate = DateTime.Now;
test.Author = User.Identity.Name;
TestEntities db = new TestEntities();
db.AddToTests(test);
db.SaveChanges();
return RedirectToAction("CreateQuestion", new { OrderNumber = 1, idTest = test.id });
}
[Authorize]
public ActionResult CreateQuestion(int OrderNumber,int idTest)
{
return View();
}
[Authorize]
[HttpPost]
public ActionResult CreateQuestion(Questions question)
{
TestEntities db = new TestEntities();
db.AddToQuestions(question);
db.SaveChanges();
return RedirectToAction("CreateQuestion", new {id = question.id, t = question.Type});
}
The problem is Create methods works right. It get parameter and adds it to DB. But similar method CreateQuestion displays message about question is null.
What do I wrong?
CreateQuestion view
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<test.su.Models.Questions>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Создать вопрос
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Создать вопрос</h2>
<% using (Html.BeginForm("CreateQuestion","Test")) { %>
<%: Html.ValidationSummary(true) %>
<fieldset>
<legend>Вопрос</legend>
<div class="editor-label">
<%: Html.LabelFor(model => model.Type,"Тип вопроса") %>
</div>
<% // List of question types
List<SelectListItem> QuestionTypes = new List<SelectListItem>();
SelectListItem t = new SelectListItem();
t.Text = "Вопрос с вариантами ответа (флажки или радиокнопки)";
t.Value = "0";
QuestionTypes.Add(t);
t = new SelectListItem();
t.Text = "Вопрос со свободным ответом (текстовое поле)";
t.Value = "1";
QuestionTypes.Add(t);
%>
<div class="editor-field">
<%: Html.DropDownListFor(model => model.Type, QuestionTypes) %>
<%: Html.ValidationMessageFor(model => model.Type) %>
</div>
<%-- <div class="editor-label">
<%: Html.LabelFor(model => model.OrderNumber,"Порядковый номер вопроса") %>
<%: Html.EditorFor(model => model.OrderNumber) %>
<%: Html.ValidationMessageFor(model => model.OrderNumber) %>
</div>--%>
<div class="editor-label">
<%: Html.LabelFor(model => model.Question,"Текст вопроса") %>
</div>
<div class="editor-field">
<%: Html.TextAreaFor(model => model.Question,2,47,"") %>
<%: Html.ValidationMessageFor(model => model.Question) %>
</div>
<%: Html.HiddenFor(model => model.idTest) %>
<%: Html.ValidationMessageFor(model => model.idTest) %>
<%: Html.HiddenFor(model => model.OrderNumber ) %>
<%: Html.ValidationMessageFor( model => model.OrderNumber) %>
<p>
<input type="submit" value="Далее" />
</p>
</fieldset>
<% } %>
</asp:Content>

This is difficult to figure out without knowing the model. Someone else may provide a better answer, but here is the only thing I can think of for now:
If your Questions model looks like this:
public class Questions
{
int Id {get;set;}
string Name {get;set;}
string Description {get;set;}
}
What you can do, for now, is alter your controller to accept the individual parameters and create the object yourself. This might help you figure out which critical property in your Model is missing.
public ActionResult CreateQuestion(string Name, string Description)
{
//make the entity yourself
Questions newQuestion = new Questions()
{
Name = Name,
Description = Description
}
//your other code here
}
Now normally MVC is smart enough to bind your individual values in your form (view) to your model, but some critical value is missing and causing you issue. Once you've figured out what that is, you can actually restore your controller back to accepting only a Questions object.
Sorry I couldn't help you more.
Good Luck.

Related

Invoke a MVC Action from a Button?

*First I want to know is it compulsory to have a view made for each and every Action method in a controller?
*How to Invoke a Action method in MVC4 when a button is clicked in another view? Do i need to have a view made for the Action method that I'm gonna invoke by pressing the button which is in another view.
Here is my code
CustomerController
public class CustomerController : Controller
{
//
// GET: /Customer/
List<Customer> CustomerCollection = new List<Customer>();
public CustomerController()
{
Customer cus = new Customer();
cus.CustomerId = 1;
cus.Name = "dath";
cus.Gender = "Male";
cus.City = "Csmbo";
CustomerCollection.Add(cus);
cus = new Customer();
cus.CustomerId = 2;
cus.Name = "Jacob";
cus.Gender = "FeMale";
cus.City = "Cosbo";
CustomerCollection.Add(cus);
cus = new Customer();
cus.CustomerId = 3;
cus.Name = "Gags";
cus.Gender = "Male";
cus.City = "NewYork";
CustomerCollection.Add(cus);
}
public ActionResult GetCustomerList()
{
return View(CustomerCollection);
}
public ActionResult GetCustomer(int id)
{
var selectedCustomer = CustomerCollection.Where(p => p.CustomerId == id).FirstOrDefault();
return View(selectedCustomer);
}
This is the Action method im gonna invoke by Pressing the button in the view called DeleteCustomer. For this Action method I haven't created any View
[HttpPost]
public ActionResult DeleteCus(int id)
{
var selectedCustomer = CustomerCollection.Where(o => o.CustomerId == id).FirstOrDefault();
CustomerCollection.Remove(selectedCustomer);
RedirectToAction("GetCustomerList", "Customer");
return View();
}
This is the DeleteCustomer Action method
public ActionResult DeleteCustomer(int id)
{
var selectedCustomer = CustomerCollection.Where(a => a.CustomerId == id).FirstOrDefault();
return View(selectedCustomer);
}
Finally this is the DeleteCustomer view that im passing the Customer seleted in DeleteCustomer Action method. And The Button is in this view. from this button i need to invoke the DeleteCus Action method(so it will remove the seleted Customer from the customerCollection List
<%# Page Language="C#" Inherits="System.Web.Mvc.ViewPage<myapp12.Models.Customer>" %>
<!DOCTYPE html>
<html>
<head runat="server">
<meta name="viewport" content="width=device-width" />
<title>DeleteCustomer</title>
</head>
<body>
<h3>Are you sure you want to delete this?</h3>
<fieldset>
<legend>Customer</legend>
<div class="display-label">
<%: Html.DisplayNameFor(model => model.Name) %>
</div>
<div class="display-field">
<%: Html.DisplayFor(model => model.Name) %>
</div>
<div class="display-label">
<%: Html.DisplayNameFor(model => model.Gender) %>
</div>
<div class="display-field">
<%: Html.DisplayFor(model => model.Gender) %>
</div>
<div class="display-label">
<%: Html.DisplayNameFor(model => model.City) %>
</div>
<div class="display-field">
<%: Html.DisplayFor(model => model.City) %>
</div>
</fieldset>
<% using (Html.BeginForm()) { %>
<p>
<input type="submit" value="Delete" onclick="location.href='#Url.Action("DeleteCus", "Customer", new { id = Model.CustomerID })'"/>
<%: Html.ActionLink("Back to List", "GetCustomerList")%>
</p>
<% } %>
</body>
</html>
The thing that i have used in onClick event is not working. I need it to work so that selected Customer can be deleted.
You should store your data in a database, otherwise you cannot save changes.
If your data is created in collections in controller constructor it should all die when the page is given and created each time the page is loaded. Database data and things like memcached persist between page calls, usual variable do not.
Here is the code without View. But remove will not work as soon as CustomerCollection is a usual variable.
[HttpPost]
public ActionResult DeleteCus(int id)
{
var selectedCustomer = CustomerCollection.Where(o => o.CustomerId == id).FirstOrDefault();
CustomerCollection.Remove(selectedCustomer);
return RedirectToAction("GetCustomerList", "Customer");
}

Display name in Data Entity framework

I'd like to know how to change the display name of a model, and customize error messages in Entity Framework. I tried the following but it didn't work.
[Required(ErrorMessage = "Required .... :")]
[Display(Name = "Name Agency : ")]
[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
[DataMemberAttribute()]
public global::System.String Nag
{
get
{
//code
}
set
{
//code
}
}
This is the code behind my form that adds data into my database. I've omitted irrelevant lines.
<% using (Html.BeginForm("addcar", "Agence", FormMethod.Post, new { #class = "search_form" }))
{ %>
<%: Html.ValidationSummary(true) %>
<div class="editor-label">
<%: Html.LabelFor(model => model.Dmcv) %>
</div>
<div class="editor-field">
<%: Html.EditorFor(model => model.Dmcv) %>
<%: Html.ValidationMessageFor(model => model.Dmcv) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Puisv) %>
</div>
<div class="editor-field">
<%: Html.EditorFor(model => model.Puisv) %>
<%: Html.ValidationMessageFor(model => model.Puisv) %>
</div>
// Similaire code
<p>
<input type="submit" value="Create" />
</p>
<% } %>
Change [Display(Name = "Name Agency")] to [DisplayName("Name Agency")] instead.
First you need to reference this:
using System.ComponentModel.DataAnnotations;
For changing the display name of the column, actually [Display(Name="Name Agency")] is OK. I'm using it in my projects.
For error message
[Required(ErrorMessage="Required...")]
I read that it is possible that this won't work if you are using the entity framework designer because the designer overwrites your changes over and over then you will need to use the metadatatype something like this:
[MetadataType(typeof(MetadataMyClass))]
public partial class myclass
{
}
//data annotations here
public class MetadataMyClass
{
[Required(ErrorMessage = "Required...")]
[Display(Name="Column Name")]
public global:: System.String Nag
{
// ... etc, etc...
}
}

DropDownListFor not binding on Edit View with repeating items (List<T>)

Here is the thing. I have an Edit view, which doesnt bind the dropdowns' value when I open it.
[NonAction]
public List<SelectListItem> VraagType() {
List<SelectListItem> l = new List<SelectListItem>();
SelectListItem a = new SelectListItem();
SelectListItem b = new SelectListItem();
a.Text = "Meerkeuze";
a.Value = "M";
b.Text = "Open";
b.Value = "O";
l.Add(a);
l.Add(b);
return l;
}
[NonAction]
public List<SelectListItem> getSchalen() {
return _db.EvalSchaals.ToList().ToSelectList(q => q.Sch_Naam, q => q.Sch_ID.ToString(), q => q.Sch_ID == -1).ToList();
}
public ActionResult Edit(int id) {
ViewData["vraagtype"] = VraagType();
ViewData["schaal"] = getSchalen();
EvalVragenBlok evb = _db.EvalVragenBloks.First(q => q.Vrbl_ID == id);
List<EvalVragen> ev = _db.EvalVragens.Where(q => q.Vrbl_ID == id).ToList();
FlatEvalVragenBlok fevb = Mapper.Map<EvalVragenBlok, FlatEvalVragenBlok>(evb);
fevb.Vragen = new List<FlatEvalVragen>();
return View(fevb);
}
this is the code from the controller.
here is the code from the Edit.aspx view
<h2>
Edit</h2>
<% using (Html.BeginForm()) {%>
<%: Html.ValidationSummary(true) %>
<fieldset>
<legend>Fields</legend>
<legend>Fields</legend>
<div class="editor-label">
<%: Html.LabelFor(model => model.Vrbl_Titel) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Vrbl_Titel) %>
<%: Html.ValidationMessageFor(model => model.Vrbl_Titel) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Sch_ID) %>
</div>
<div class="editor-field">
<%: Html.DropDownListFor(model => model.Sch_ID, ViewData["schaal"] as List<SelectListItem>, "Selecteer een schaal...") %>
<%: Html.ValidationMessageFor(model => model.Sch_ID) %>
</div>
<%= Html.ValidationMessageFor(model => model.Vragen) %>
<table id="vragentbl">
<tr>
<th>
</th>
<th>
Vraag
</th>
<th>
Soort
</th>
</tr>
<% if (Model.Vragen != null) { %>
<% for (int i = 0; i < Model.Vragen.Count; i++) { %>
<tr>
<td>
<%=i + 1%>
</td>
<td>
<%= Html.TextBoxFor(model => model.Vragen[i].Evvr_Vraag, new { style = "width:400px" })%><br />
<%= Html.ValidationMessageFor(model => model.Vragen[i].Evvr_Vraag)%>
</td>
<td>
<%= Html.DropDownListFor(model => model.Vragen[i].Evvr_Type, ViewData["vraagtype"] as List<SelectListItem>, new { style = "width:95px" })%><br />
<%= Html.ValidationMessageFor(model => model.Vragen[i].Evvr_Type)%>
</td>
</tr>
<% }
} %>
<tr>
<td>
</td>
<td>
<a id="addnew" href="#">Voeg extra keuze toe</a>
</td>
<td>
</td>
</tr>
</table>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
<% } %>
I have 2 List 's. 1 of them is in the non-repeating part of the form (Schalen), the other one (VraagType) is Inside the repeating part.
for Schalen, everything works fine. i open the edit view, and all fields are filled in like it should be. the Vrbl_Titel has its value, and the dropdown of Sch_ID has the value it received from the object which i sent with the view, which came from the DB.
The problem lies in the repeating part.
the textbox for model.Vragen[i].Evvr_Vraag get's its value, and the dropdown for model.Vragen[i].Evvr_Type is shown, however, this dropdown does not get the value which was sent in the object. it keeps it's default standard value, which is the first item in the 'selectlist'
how do i get my value from my 'Vragen' object, into the dropdown. if i put the value in a simple textbox
<%= Html.TextBoxFor(model => model.Vragen[i].Evvr_Type)%>
then the textbox does get the value. so the problem is that the dropdownvalue doesnt change form it's initial value... bug in MVC?
just for info, this is how the object(s) look sent to the view:
namespace MVC2_NASTEST.Models {
public partial class FlatEvalVragenBlok {
public int Vrbl_ID { get; set; }
public int Sch_ID { get; set; }
public string Vrbl_Titel { get; set; }
public List<FlatEvalVragen> Vragen { get; set; }
}
}
namespace MVC2_NASTEST.Models {
public partial class FlatEvalVragen {
public int Evvr_ID { get; set; }
public int Vrbl_ID { get; set; }
public int Evvr_rang { get; set; }
public string Evvr_Vraag { get; set; }
public char Evvr_Type { get; set; }
}
}
It seems this is really a bug or at least inconsistency in ASP.NET MVC 2. I have examined its source and found what InputHelper() method called from TextBoxFor() helper receives default value calculated with
ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData).Model
But SelectInternal() method called from DropDownListFor() helper receives only a name of a control found with ExpressionHelper.GetExpressionText() method.
So SelectInternal() tries to find default value using ViewData.Eval() method from MVC 1. It's known what this method isn't able to extract values from arrays by numeric index.
So in your case are applicable
<%: Html.DropDownListFor(model => model.Sch_ID) %>
<%= Html.TextBoxFor(model => model.Vragen[i].Evvr_Type)%>
but not
<%: Html.DropDownListFor(model => model.Vragen[i].Evvr_Type) %>
because it's equivalent to
<%: Html.DropDownList("Vragen[" + i + "].Evvr_Type") %>
At the same time I want to emphasize again what
<%= Html.TextBoxFor(model => model.Vragen[i].Evvr_Type)%>
isn't equivalent to
<%= Html.TextBox("model.Vragen[" + i + "].Evvr_Type")%>
because latter even in MVC 2 can't bind default value.
Possible workarounds
First. Since SelectInternal() also checks ModelState dictionary you can fill this dictionary before returning the view.
for (int i=0; i < fevb.Vragen.Count(); i++)
ModelState.Add("Vragen[" + i + "].Evvr_Type", new ModelState
{
Value = new ValueProviderResult(fevb.Vragen[i].Evvr_Type, null,
CultureInfo.CurrentCulture)
});
This will be done by MVC itself after from post, so you should do it manually only first time.
Second. Instead of
<%= Html.DropDownListFor(model => model.Vragen[i].Evvr_Type,
ViewData["vraagtype"] as List<SelectListItem>)%>
use
<%= Html.DropDownListFor(model => model.Vragen[i].Evvr_Type,
new SelectList(ViewData["vraagtype"] as IEnumerable, "Value", "Text",
Model.Vragen[i].Evvr_Type))%>
ViewData["vraagtype"] in this case doesn't have to contain objects of SelectListItem, any IEnumerable is enough. You may check SelectList() method description in case of need.

Create view is posting null objects

Should be an easy question to answer.
I am trying to create an object in a view. The class that contains the object consists of a User class and a password.
When I click on the submit button, the Controller picks up null values for Password and User.
See below the Container class, the Controller and the View;
public class UserExtended
{
public UserITOC User { get; set; }
public string Password { get; set; }
}
[Authorize]
public ActionResult Create()
{
return View(new UserExtended());
}
//
// POST: /Dinners/Create
[Authorize(Roles = "Administrator")]
[HttpPost]
public ActionResult Create(UserExtended user)
{
if (ModelState.IsValid)
{
// Create user in the User datatable
SqlUsersRepository sqlRepository = new SqlUsersRepository();
ITOCEntities db = new ITOCEntities();
db.UserITOCs.AddObject(user.User);
// Create user as an authenticated user within the Reader role.
int i = user.User.EmailAddress.IndexOf('#') - 1;
string userName = user.User.EmailAddress.Substring(0, i);
string email = user.User.EmailAddress;
Membership.CreateUser(userName, user.Password, email);
Roles.AddUserToRole(userName, "Reader"); // Automatically assigned as a Reader
}
return View(new UserExtended());
}
" %>
Create
<h2>Create</h2>
<% using (Html.BeginForm()) {%>
<%: Html.ValidationSummary(true) %>
<fieldset>
<legend>Fields</legend>
<div class="editor-label">
<%: Html.LabelFor(model => model.User.Forename) %>
</div>
<div class="editor-field">
<%: Html.EditorFor(model => model.User.Forename)%>
<%: Html.ValidationMessageFor(model => model.User.Forename)%>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.User.Surname) %>
</div>
<div class="editor-field">
<%: Html.EditorFor(model => model.User.Surname)%>
<%: Html.ValidationMessageFor(model => model.User.Surname)%>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.User.EmailAddress) %>
</div>
<div class="editor-field">
<%: Html.EditorFor(model => model.User.EmailAddress)%>
<%: Html.ValidationMessageFor(model => model.User.EmailAddress)%>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Password) %>
</div>
<div class="editor-field">
<%: Html.EditorFor(model => model.Password)%>
<%: Html.ValidationMessageFor(model => model.Password) %>
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
<% } %>
<div>
<%: Html.ActionLink("Back to List", "Index") %>
</div>
Extremely simple solution:
Change your action-signature from
public ActionResult Create(UserExtended user)
to
public ActionResult Create(UserExtended UserExtended)
That way the ModelBinder will know how to reassemble the object from Request.
Hope this helps!
I had a very similar problem, but found in my case that I had to match the database table name rather than the type name
Name of the type: NonTradingDay
Database table name: dbo.NonTradingDays (had been pluralized)
Create Method:
[HttpPost]
public ActionResult Create(NonTradingDay NonTradingDays)
{
if (ModelState.IsValid)
{
db.NonTradingDay.Add(NonTradingDays);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(NonTradingDays);
}
I had tried 'NonTradingDay', but still got null; I then looked at the database table name, tried 'NonTradingDays' and the mapping was made (the argument was no longer null).
I think this was because I had the database context as:
public DbSet<NonTradingDay> NonTradingDay { get; set; }
Rather than:
public DbSet<NonTradingDay> NonTradingDays { get; set; }
you are returning a new instance of UserExtended class
return View(new UserExtended());
instead return the object you get as the parameter
return user

MVC Validation Not Working In Web Forms Project

I have the following code in my aspx view page:
<% using (Html.BeginForm())
{
%>
<div>
CustomerCode:
<%= Html.TextBoxFor(x=> x.CustomerCode) %>
<%= Html.ValidationMessageFor(x => x.CustomerCode)%>
and this code in my model:
public class MyModel
{
[Required(ErrorMessage="customer code req")]
[StringLength(2,ErrorMessage="must be 2 u idiot")]
public string CustomerCode {get; set;}
Though if I enter more than 2 charachters in the textbox and submit the page, in the controller when I do:
if (ModelState.IsValid)
It always says its valid? What am I missing? I have put this MVC project inside a Web Forms project but the MVC project works fine, its just the validation which is not working, any ideas? Thanks.
Make sure that the controller action accepts the model as parameter:
public ActionResult SomeAction(MyModel model)
{
if (ModelState.IsValid)
{
}
return View();
}
Now if you invoke:
http://example.com/myapp/home/someaction?customercode=123
The model should not be valid.
Hmm, it works for me on a test page with the following
public ActionResult Test()
{
MyModel model = new MyModel();
return View(model);
}
[HttpPost]
public ActionResult Test(MyModel model)
{
if (ModelState.IsValid) { }
return View(model);
}
<% using (Html.BeginForm()) {%>
<%: Html.ValidationSummary(true) %>
<fieldset>
<legend>Fields</legend>
<div class="editor-label">
<%: Html.LabelFor(model => model.CustomerCode) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.CustomerCode) %>
<%: Html.ValidationMessageFor(model => model.CustomerCode) %>
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
<% } %>
public class MyModel
{
[Required(ErrorMessage = "customer code req")]
[StringLength(2, ErrorMessage = "must be 2 u idiot")]
public string CustomerCode { get; set; }
}

Resources