Reputation: 11
i am new to mvc way to web development , i have the home controller with two actions index and save and a model called MyModel and a static class SomeData which will have MyModel as property - SomeData is a static class with static property MyModel (which is of type of the model class that i have ) and a static method getdata() , which returns the reference to the ModelClass
public static MyModel MyModels;
static SomeData()
{
MyModels = new MyModel { SomeValue = 100 , SomeString="ComingFromSomeData"};
}
public static MyModel getdata()
{
return MyModels;
}
below are the two action methods , now what happens is on startup action index calls the getdata method and binds the model and i can see the data but when i change the values and click save , it comes back to action "Save" and i modify the passed properties (adding 100 and adding "bro") and try to bind the model again to the "Save" view (the save view is similar to Index view , except that it also has an ActionLink , which navigates to Index view again) , the problem is that on "Save" view , i am not able to see the changes values but if i click on actionlink and go to Index view again , i can see the changed values (which have been stored in the static class)
public ActionResult Index()
{
MyModel M = SomeData.getdata();
return View(M);
}
[HttpPost]
public ActionResult Save(ValidationPractice.Models.MyModel M)
{
if (ModelState.IsValid)
{
SomeData.MyModels.SomeValue = M.SomeValue + 100;
SomeData.MyModels.SomeString = M.SomeString+" bro";
}
MyModel whosemodel = SomeData.getdata();
//return View("Index", whosemodel)
return View(whosemodel);
}
and model as following
public class MyModel
{
private int _somevalue;
[Required]
public int SomeValue
{
get { return _somevalue; }
set { _somevalue = value; }
}
[Required]
public string SomeString
{
get;
set;
}
}
and the index view as following
@model ValidationPractice.Models.MyModel
<form action="Home\Save" method="post">
<label>SomeLabel</label>  
@Html.EditorFor(model => model.SomeValue)
@Html.EditorFor(model=>model.SomeString)
<input type="submit" />
</form>
and the save view as following -
@model ValidationPractice.Models.MyModel
<form action="Home\Save">
<label>SomeLabel</label>  
@Html.EditorFor(@model => model.SomeValue)
@Html.EditorFor(@model=>model.SomeString) <br />
@Model.SomeString
@Model.SomeValue<br />
<input type="submit" />
@Html.ActionLink("Clicktoreturn", "Index");
The point to note is that @Model.SomeString ,@Model.SomeValue returns the updated values which is 200 and ComingFromSomeDataBro but the bound model lambda expression @model=> model.SomeString doesn't ...why is it happening ?
Upvotes: 1
Views: 1365
Reputation: 93464
First, avoid almost anything static in a web application (with a few exceptions, like extension methods).
The reason is that statics are shared not just between multiple web requests by the user, but also between users. So if you have two users accessing your website, they will both be accessing that static data and you will get unpredictable results.
Second, you should be using the Html.BeginForm helper rather than using the html form tags.
Third, you don't have your form tags set right. "Home\Save" is the wrong kind of slash to begin with, it should be "Home/Save" and it's also url relative. So if your current URL is /Home/Index then when you click the submit button it will try to submit to /Home/Index/Home\Save. This may work if you're using the default page behavior where / actually renders /Home/Index, but as soon as you're at a different url it will fail.
This is why you should be using the helpers, because they figure out the proper paths for you.
Upvotes: 1
Reputation: 18977
Update: you are using model binding in wrong way:
@Html.EditorFor(@model => model.SomeValue) //
@Html.EditorFor(@model=>model.SomeString) // What is @model...?! just model!
Correct your Svae view like the following:
@model ValidationPractice.Models.MyModel
<form action="Home\Save">
<label>SomeLabel</label>  
@Html.EditorFor(model => model.SomeValue)
@Html.EditorFor(model=>model.SomeString) <br />
@Model.SomeString
@Model.SomeValue<br />
<input type="submit" />
@Html.ActionLink("Clicktoreturn", "Index");
And, next time, remember to add enough related code to your question ;)
Upvotes: 0
Reputation: 1504
Review your SomeData.. use SomeData.Init() is explicitly initialize MyModels before usage.. give that a try.
public static class SomeData
{
public static MyModel MyModels;
public static Boolean Init()
{
SomeData.MyModels = new MyModel { SomeValue = 100 , SomeString="ComingFromSomeData"};
return true;
}
public static void AddValue(int AValue)
{
SomeData.MyModels.SomeValue += AValue;
}
public static void AppendString(string AString)
{
SomeData.MyModels.SomeString += AString;
}
public static MyModel getData()
{
return SomeData.MyModels;
}
}
Upvotes: 0