Assuming you're trying to set values on 'newPlayer', then you can replace this.
Up vote 3 down vote favorite share g+ share fb share tw.
I just started working with MVC3 a few weeks ago and being young in the programming ladder I'm still learning quite a bit. I've recently been working with Models, using TextBoxFor and other helpers to populate properties for my "Character" Model. Essentially what I'm trying to do is define a model and then pass it to my controller, however any property that I have defined as a static value in my Model is being passed as a null value on runtime.
Below are some snippets of the parts needed to understand whats going on.. Character. Cs - Model // Instances of manipulated objects. OtReal db = new otReal(); public player newPlayer = new player(); public byte conditions { get { return newPlayer.
Conditions; } set { byte array1 = null; array1 = new byte16 * 12 * 3; newPlayer. Conditions = array1; } } CharacterController. Cs AcceptVerbs(HttpVerbs.
Post) public ActionResult Submit(Character c) { // Add a new character to the database via LINQ to Entities. OtReal. AddToplayers(c.
NewPlayer); otReal.players. AddObject(c. NewPlayer); otReal.SaveChanges(); return View(); } The only helpers I have in my View are the ones that the user actually needs to interact with.
If I go into my controller and set the values there they will get set to the correct value and it will insert. Any help would be greatly appreciated! Index.
Cshtml - View @using (Ajax. BeginForm("Submit", new AjaxOptions { OnComplete = "done" })) { @Html. ValidationSummary(true) New Character Information @Html.
LabelFor(model => model. Name, "Character Name") @Html. EditorFor(model => model.
Name) @Html. ValidationMessageFor(model => model. Name) @Html.
LabelFor(model => model. TownList) @* @Html. DropDownList("TownList", ViewData"TownList" as SelectList)*@ @Html.
DropDownListFor(model => model. TownList, ViewData"TownList" as SelectList) @Html. ValidationMessageFor(model => model.
TownList) @Html. LabelFor(model => model. Sex) @Html.
DropDownListFor(model => model. Sex, ViewData"sex" as SelectList) @Html. ValidationMessageFor(model => model.
Sex) @Html. LabelFor(model => model. Vocation) @Html.
DropDownList("VocList", ViewData"VocList" as SelectList) @Html. ValidationMessageFor(model => model. Vocation) } Basically what I'm trying to do here is create my model that has a bunch of base values that every 'Character' will have in the database table.
This way when a player creates his/her character he/she will be able to enter a Character Name, Sex, Class(Vocation), Starting Town and all the other values will be populated behind the scenes and passed to the controller for creation. C# asp.net web-development asp.net-mvc-3 link|improve this question edited Apr 18 '11 at 1:28 asked Apr 17 '11 at 22:30jhartzell9418 67% accept rate.
1 You will probably need to show us some View code as well. Specifically the form that POSTs to CharacterController.Submit() – Chris Sainty Apr 17 '11 at 23:28 Can you supply some more information, what exactly are you trying to do, what error are you getting and on what line of code does that error occur? – NickLarsen?
Apr 17 '11 at 23:29 The error that I'm getting is that "conditions" cannot be null because it is a NOT NULLABLE column in SQL. However the above property you see is the base setup for every property I have I pretty much either set it statically or set it based on a "value" that is passed from the view. – jhartzell Apr 18 '11 at 1:38.
Assuming you're trying to set values on 'newPlayer', then you can replace this public player newPlayer = new player(); with this public player newPlayer { get; set; } The default model binder will create everything from the form on post-back--there's no need to instantiate it in the model.
I really like the concept behind this idea ataddeini, however I'm not entirely sure how to set it up with my current properties as I am setting values of an instantiated object as to where your above example is a bit vague to me. I would love to learn more about it because it seems like exactly what I need. – jhartzell Apr 18 '11 at 1:29 Ah, I think I understand your problem now.
I would recommend using a separate view model for the portions of the data that the user can edit, and then combine this with any shared data before you persist to the database. You end up with more classes, but in the long run it keeps things cleaner. Here's some more information on the approach.
Best of luck. – ataddeini Apr 18 '11 at 2:00 Yea, I will probably take this approach. As a temporary fix just to test more with MVC I've just setup the Character with a constructor that sets all those values upon object creation.
– jhartzell Apr 18 '11 at 2:36.
Created a constructor for my model and inside the constructor I set the default values via the newly instantiated player model. Temp fix until I read into ataddeini's solution. Thanks everyone!
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.