My strongly typed View inherits from a "Person" object that is created with Linq to SQL. In my "Edit" View, i have to display of course old values:
<%= Html.TextBox("FirstName") %>
"FirstName" is NCHAR, so it need to be trimmed. So i ended up with:
<%= Html.TextBox("FirstName", Model.FirstName.Trim()) %>
and this works. But when form is submitted (after POST) and some errors occur, i need to show it again:
[AcceptVerbsAttribute(HttpVerbs.Post), Authorize(Roles = "office"), HandleError]
public ActionResult Edit(Models.Person person)
{
if (!(_personService.ValidatePerson(person))) // Persona non valida
{ return View(person); }
}
If for some reason the user left the textbox "FirstName" blank, the resulting property Person.FirstName become null and Model.FirstName.Trim() throws an Exception (Object reference not set to an instance of an object).
Any way to modify the bind and have all string trimmed by default? Or any ideas to how fix this?
Update: seems confirmed to be a MVC 2 behaviour.. still looking for a good way to handle this. Actually using an extension method:
public static string TrimOrDefault(this string value)
{
return value != null ? value.Trim() : string.Empty;
}
Something is odd here.
Model.FirstName shouldn't be null; the model binder is smart enough to set an empty input field ( textbox ) to "". Make sure your property names match up between your model and the textboxes your using.
Which version of MVC are you using? 1 or 2? I'm running a MVC 1 version in VS 2008 and the only way I can get FirstName to be null is by not including it in the form at all.
I could see if you initial GET Edit view threw this error and you had FirstName set to nullable in your dbml and database but since it is a Post this doesn't make sense to me right now. ;)
Update:
I've confirmed this:
With an empty form:
VS 2008 - Mvc 1 - FirstName = ""
VS 2010 - Mvc 2 - FirstName = null
Uh oh... Thats going to break a lot of code...
The Code:
View ( same for both ):
<% using (Html.BeginForm()) {%>
<fieldset>
<legend>Fields</legend>
<p>
<label for="FirstName">FirstName:</label>
<%= Html.TextBox("FirstName", Model.FirstName) %>
<%= Html.ValidationMessage("FirstName", "*") %>
</p>
<p>
<label for="LastName">LastName:</label>
<%= Html.TextBox("LastName", Model.LastName) %>
<%= Html.ValidationMessage("LastName", "*") %>
</p>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
<% } %>
VS 2010 - Mvc 2
public class HomeController : Controller
{
public ActionResult Index()
{
ViewData["Message"] = "Welcome to ASP.NET MVC!";
return View();
}
public ActionResult About()
{
return View();
}
public ActionResult AddPerson()
{
var person = new Person();
return View(person);
}
[HttpPost]
public ActionResult AddPerson(Person person)
{
return View(person);
}
}
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
VS 2008 - Mvc 1
public class HomeController : Controller
{
public ActionResult Index()
{
ViewData["Message"] = "Welcome to ASP.NET MVC!";
return View();
}
public ActionResult About()
{
return View();
}
public ActionResult AddPerson()
{
var person = new Person();
return View(person);
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult AddPerson( Person person )
{
return View(person);
}
}
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
No clean fix atm. The model binder is actually setting those properties to null. Set First and Last to "" in the constructor and exclude those properties from binding: [Bind(Exclude="FirstName, LastName")] they stay "".
Is this documented someplace?
Try a custom modelbinder.
See this : ASP.NET MVC: Best way to trim strings after data entry. Should I create a custom model binder?
In Models.Person have the default value be String.Empty which should elminate the null error at least.
If you want to have it automated, then you can add that into your dbml file. There is a designer.cs file attached and each field has a getter and setter. you can add code in there if you wish.
you could also create a partial class based on your table and handle the trim within that. it means that if you make a change to the dbml file then you don't lose your changes.
if you're not using a dbml file then let us know.
EDIT
If you have a class called person in your dbml file then this will already be declared as partial.
create another class, in the same project and do something like the following;
public partial class Person
{
public string NewName
{
get { return this._name.Trim(); }
}
}
So from then on in you can use .NewName instead of name and it will come back trimmed. You can also add code in there to ensure it's not null, not red, not whatever and return the appropriate value.
I'm not aware of an extension method that does this, if someone else does then please let me know.
I create a simple HTML helper for fields that COULD be null.
public static string TrimOrDefault(string value)
{
return (value == null ? "" : value.Trim());
}
And then in your code you can use
<%= Html.TextBox("FirstName", Helpers.TrimOrDefault(Model.FirstName)) %>
It's re-usable for future nullable fields and reads easily.
Related
its me... yet again!
Ive got these class,
public class PrankTargetArgumentViewModel
{
public PrankTarget Target { get; set; }
public PrankDefinition Prank { get; set; }
public List<PrankArgument> Arguments { get; set; }
}
public class PrankArgument
{
public string Name { get; set; }
public string Value { get; set; }
}
and what I'm doing is - if this current ParkDefinition needs arguments them im doing an ActionRedirect on the save to another Action which should handle the gathering of the Arguments
My Action result is like this..
public ActionResult PrankArguments()
{
PrankInstance currentInstance = SessionContext.CurrentPrankInstance;
if (currentInstance == null)
throw new ArgumentNullException("currentInstance");
PrankTargetArgumentViewModel model = new PrankTargetArgumentViewModel();
model.Prank = currentInstance.Prank;
model.Target = currentInstance.Target;
string[] args = model.Prank.Arguments.Split('|');
model.Arguments = new List<PrankArgument>();
foreach (string s in args)
{
model.Arguments.Add(new PrankArgument { Name = s, Value = s });
}
return View(model);
}
my http post method is just an empty method with the parameter of PrankTargetArgumentViewModel
[HttpPost]
public ActionResult PrankArguments(PrankTargetArgumentViewModel model)
{
return View();
}
My HTML is like this..
#using (Html.BeginForm())
{
#Html.EditorFor(x => Model)
<p>
<input type="submit" value="Create" />
</p>
}
So my problem is this, on the PrankArguments(PrankTargetArgumentViewModel model) post back action, the model param is always null.. I've filled the object with values on the load so I guessed they would be there on the post back with the new arguments that I added.
so the flow goes like this.
Create Prank
If prank needs arguments then load ActionResult PrankArguments()
Add extra arguments to an already poplulated object.
save, Call ActionResult PrankArguments(PrankTargetArgumentViewModel model)
-- this is where the problem is, the model parameter is passed back as null.
Ive had this problem quite a few times and always just given up but im not going to let that happen this time!
any help would be great! cheers, Ste!
Ps. If you need anymore of my code just let me know.
EDIT - Removed view bag debug properties!
I think if I understand you correctly if your view is strongly typed to PrankTargetArgumentViewModel then all you have to do to retrieve the values is:
[HttpPost]
public ActionResult PrankArguments()
{
var pta = new PrankTargetArgumentViewModel();
TryUpdateModel(pta);
}
After reviewing my own code - I noticed that I didn't need the entire PrankTargetArgumentViewModel and a simple List of Arguments would have been fine.
I alterd my PrankArguments view to take an IEnumerable and used;
#using (Html.BeginForm())
{
#Html.EditorForModel()
<p>
<input type="submit" value="Finish" />
</p>
}
then had my post back method signature like this
[HttpPost]
public ActionResult PrankArguments(IEnumerable<PrankArgument> arguments)
which worked exactly how I wanted.
Thanks for all the suggestions guys.
When I display a session value in a master page (<%: Session["companyname"].ToString() %>) the following information is displayed on the page { CompanyName = TestCompany}. How do I get just the value?
Thanks for your help!
If you can show the code where the value is stored in the session, it's more likely that I could help. I would suggest, though, that you might want to reconsider using the value from the session directly in your view. It would be better, in my opinion, to have a base view model that all of your view models derive from that has the CompanyName property, and any other common properties required by your master page, on it. Your master page, then, could be strongly-typed to the base view model and you could use the values from the model. I've used this pattern with good success on a couple of projects. Couple it with a base controller where the common properties are populated for view results in OnActionExecuted(), it can be very effective in both reducing code duplication and the use of magic strings in your views.
Model:
public abstract class CommonViewModel
{
public string CompanyName { get; set; }
public string UserDisplayName { get; set; }
...
}
Controller:
public abstract class BaseController
{
public override void OnActionExecuted( ActionExecutedContext filterContext )
{
if (filterContext.Result is ViewResult)
{
var model = filterContext.ViewData.Model as CommonViewModel;
if (model != null)
{
model.CompanyName = Session["CompanyName"] as string;
model.UserDisplayName = Session["UserDisplayName"] as string;
}
}
}
}
Master Page:
<%# Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage<Foo.CommonViewModel>" %>
<!-- ... -->
<div>
<%: Model.CompanyName %>
</div>
<!-- ... -->
<div>
<%: Model.UserDisplayName %>
</div>
I am trying to use the Html.ListBoxFor helper to show a list box and return the selected Id. Is there a problem with the dataValueField not being a string?
If the SelectList contained in the model uses integers as the dataValueField then I get a "Value cannot be null - Parameter name: Source" exception raised when the list is rendered in the view.
If the Id is changed to a string then everything works and the selected Id is passed back to the view.
Any ideas?
Here is the controller (based on a cut down new project)
namespace Mvc2.Controllers
{
public class ViewModel
{
public int TestId { get; set; } // if this is a string it works ok
public SelectList ListData {get; set;}
}
[HandleError]
public class HomeController : Controller
{
public ActionResult Index()
{
var model = new ViewModel();
model.TestId = 1; // code corrected after Lazarus' comment
var lst = new[] { new { Id = 1, Name = "cat" }, new { Id = 2, Name = "dog" } };
model.ListData = new SelectList(lst, "Id", "Name");
return View("TestView", model);
}
public ActionResult TestSubmit(ViewModel returnedModel)
{
int i = 99; // break here - returnedModel has correct TestId when declared as string
}
}
}
here is the View - crashes on the ListBoxFor line
<%using (Html.BeginForm("TestSubmit", "Home")) { %>
<%=Model.TestId %><br />
<%=Html.ListBoxFor(m => m.TestId, Model.ListData) %>
<br />
<input type="submit" value="Save" />
<%} %>
The expression you are passing for the selected values needs to be IEnumerable because ListBoxFor supports multiple selected items.
Answering my own question;
I am unconviced by the comments that this might be a bug which is waiting to be fixed because I get it in RC2 and in MVC 1 (I copied the code back to a project in that release).
Anyway I have implemented a work around for now which is to:-
(a) Add a dummy string version of the Id to the model (TestId)
public class ViewModel
{
public string TestId { get; set; } // dummy Id as a string
public List<DataToShow> Data { get; set; }
public SelectList ListData {get; set;}
}
(b) Display the list but retrieve the value as the dummy TestId - note that the list still dumps the data values as integers!
<%=Html.ListBoxFor(m => m.TestId, Model.ListData) %>
(c) Copy the dummy string value into its proper integer location in the action
public ActionResult TestSubmit(ViewModel returnedModel)
{
MyModel.DataId = Int32.Parse(returnedModel.TestId);
Hope this is of some Help.
This is a known issue with ASP.NET MVC 2. It should be fixed in the March release.
I am trying to understand something a bit better with being new to C#, .NET 3.5 and MVC.
I am running through the MVC NerdDinner example and if you look at the ViewModel here: http://nerddinnerbook.s3.amazonaws.com/Part6.htm#highlighter_662935
You can see the Country list and how it gets populated, this seems to work fine but I tried to do a similar thing below using LINQ and I am having problems, with the SelectList approach even though it inherits from the IEnumerable interface.
I have got a task table with a foreign key to a status table. The below code gives me a NullReferenceException when I do a GET on a create action. I can see that an anonymous task object would not have a status set.. so I probably need to check for it, but I dont understand how this is not done for the NerdDinner example??
public class TaskViewModel {
// Properties
public Task Task { get; private set; }
public SelectList Status { get; private set; }
// Constructor
public TaskViewModel(Task task) {
TaskRepository taskRepo = new TaskRepository();
Task = task;
Status = new SelectList(taskRepo.GetStatus(), Task.Status.description);
}
}
//
// GET: /Tasks/Create
public ActionResult Create()
{
Task task = new Task();
return View(new TaskViewModel(task));
}
//Code from TaskRepository
private TaskManagerDataContext db = new TaskManagerDataContext();
public IQueryable<Status> GetStatus() {
return from status in db.Status
orderby status.description
select status;
}
I did another approach using LINQ for the type dropdown and the population of the drop down works but I am yet to test if it selects the correct value once a post is made and the details view is returned. I am also wondering whether this should some how be moved into my repository rather than have a class in my controller doing this sort of thing??
Here is the code:
//In TaskViewModel Class
public IEnumerable<SelectListItem> Types { get; private set; }
//In TaskViewModel constructor
IList<NPType> types = taskRepo.GetTypes().ToList();
Types =
from type in types
select new SelectListItem {
Selected = (type.typeId == task.typeId),
Text = type.description,
Value = type.typeId.ToString()
};
//The TaskForm partial View that is used for the Create action of the TaskController
<p>
<label for="type">type:</label>
<%= Html.DropDownList("Type", Model.Types)%>
<%= Html.ValidationMessage("type", "*") %>
</p>
<p>
<label for="status">status:</label>
<%= Html.DropDownList("Status", Model.Status)%>
<%= Html.ValidationMessage("status", "*") %>
</p>
and the TaskForm view inherits System.Web.Mvc.ViewUserControl
What's in your task constructor? What is the value of .typeId on a newly created task? Is it a null reference?
For the view model sent to your Create view, you shouldn't be trying to set the selected list item unless your task constructor (or other initialization code) sets default values for those properties. If task.typeId is null, then your code that is building the select list will get an error.
I understand that I will get a null value for the type or status if I dont add a value to the newly created task. What I dont understand and which I didnt make clear is the below.
You can see the view model has a Countries property, and its selected value is set to Dinner.Country.. now Dinner.Country is not being set in the create action.. so how come this does not give a null exception?
//viewmodel code
public DinnerFormViewModel(Dinner dinner) {
Dinner = dinner;
Countries = new SelectList(PhoneValidator.Countries, Dinner.Country);
}
//controller code
public ActionResult Create() {
Dinner dinner = new Dinner() {
EventDate = DateTime.Now.AddDays(7)
};
return View(new DinnerFormViewModel(dinner));
}
//view code
<p>
<label for="Country">Country:</label>
<%= Html.DropDownList("Country", Model.Countries) %>
<%= Html.ValidationMessage("Country", "*") %>
</p>
My attempt at trying to understand this better.
//controller code creating a select list in the viewmodel class.
//taskRepo.GetStatus() returns an IQueryable<Status>
Status = new SelectList(taskRepo.GetStatus(), Task.Status);
//MVC Framework SelectList class constructor and ToEnumerable method
public SelectList(IEnumerable items, string dataValueField, string dataTextField, object selectedValue)
: base(items, dataValueField, dataTextField, ToEnumerable(selectedValue)) {
SelectedValue = selectedValue;
}
private static IEnumerable ToEnumerable(object selectedValue) {
return (selectedValue != null) ? new object[] { selectedValue } : null;
}
I can see that SelectList uses its base class of MultiSelectList and that constructor is here:
public MultiSelectList(IEnumerable items, string dataValueField, string dataTextField, IEnumerable selectedValues) {
if (items == null) {
throw new ArgumentNullException("items");
}
Items = items;
DataValueField = dataValueField;
DataTextField = dataTextField;
SelectedValues = selectedValues;
}
When I run this project the html is given as:
<select id="Status" name="Status"><option>NPTaskManager.Models.Status</option>
<option>NPTaskManager.Models.Status</option>
<option>NPTaskManager.Models.Status</option>
<option>NPTaskManager.Models.Status</option>
</select>
Which is to be expected.
If I change the controller code to:
Status = new SelectList(taskRepo.GetStatus(), Task.Status.statusId.ToString(), Task.Status.description);
Then I get a NullReferenceException. Since this is not an ArgumentNullException It seems to me that the root of the exception is not the first SelectList argument. What I am trying to understand is how this all occurs?
Is it because Task.Status needs to be added to Task in the create action of the controller?
I will change this code to use the LINQ approach that I used for the task above, all I am trying to achieve now is some understanding of what is going on.
I've been playing with ASP.NET MVC and ran into something I can't figure out.
Suppose I have an object like this :
public class TestObject
{
public string Name { get; set; }
public int Age { get; set; }
}
And a view page (Create.aspx) like this :
<form action="/Create" method="post">
<p>
<%=Html.TextBox("Name") %>
</p>
<p>
<%=Html.TextBox("Age")%>
</p>
</form>
And on my controller I have these actions :
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Create()
{
return View(new TestObject { Name = "DefaultName", Age = 10 } );
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(TestObject o)
{
o.Name = "ChangedNameToSomethingElse";
o.Age = 15;
return View(o);
}
The Html.TextBox() method always genereates the textboxes with the default values, even after the postback, where the object is passed back with different properties on its values. Now, granted, I can't think of a real world example why I'd want to do such a thing but I still don't understand why I always end up having textboxes populated with the model's values that were set on the Create action with the AcceptVerbs(HttpVerbs.Get) attribute.
Note : I've tried Html.TextBox("Name", Model.Name) but the result is still the same. And I verified that the Create action with AcceptVerbs(HttpVerbs.Post) actually runs, by passing a value via ViewData to the View.
Also, the udated value is displayed when I output the value with <%=Model.Name %> but again, not on the textbox.
Is there something obvious I'm missing, or is there a reasoning behind this behaviour?
If you bind the result of a post request through the declaration of the method or by UpdateModel or TryUpdateModel to an object such as TestObject, a property called ModelState will get filled in with these values. The HTML helpers such as Textbox will always bind to modelstate over an explicitly passed model object.
I know this was answered a long time ago, but this is more targeted solution that works for me.
[HttpPost]
public ActionResult Create(TestObject o) {
ModelState.Remove("Name");
o.Name = "ChangedNameToSomethingElse";
ModelState.Remove("Age");
o.Age = 15;
return View(o);
}
Hope this helps someone out there.
Try this one:- (hope it will work for you)
[HttpPost]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(TestObject o) {
ModelState.Clear();
o.Name = "ChangedNameToSomethingElse";
o.Age = 15;
return View(o);
}