I saw some unexpected behavior at the end of this week. I made MVC a super simple page to display to anyone learning someone.
The model contains an item, 'string text {found; Set up;} '' '
Find only two methods for the index ()' and '[HTTP post] in the index (string text) index (), I created a model, and "enter some text Do "and set the value of the text (model)
In the CSTML file, my two items were:
@html. Textboxfor (M => M.Test)
and
@models. Text
which will only display the value of the text.
And submit a submit button. (It has been sent back to the index)
Here it becomes weird in the post method, I created a new model, which is the "text" property "+" !!
I was hoping that if I set the text in 'A', and hit the button, it would be 'one !!' In the text box as well as 'a !!'
However, instead, the value of the edit box remains the same, and the value of the @model. The text changes!
It will also be if you execute the GET URL with the text = A - no matter what you pass in your model, it will leave the value used in the text box / text array and ' A 'will show! But, the value of the text from the @model. In the model passed to see the text, the values will be displayed correctly.
It seems that they had to go out to break it - instead of this model url / post data.
Wtf?
Controller:
Using the system; Using System.Collections.Generic; Using System.Linq; Using System.Web; Using System.Web.Mvc; Using TestApp.Models; Namespace TestApp.Controllers {Public class homeController: Controller {public performance index} {TestModel model = New TestDodel {text = "Enter your text here!} Return view (model);} [HTP post] Public ActionRashult Index (Testodel model) {Model.text = model.text + "!!"; Return View (model);}}}
See:
@ TestApp's usage Using model @modelTestodel @Usage (HTML.benform ("index "," Home ")) {@ Html.TextAreaFor (M => M. Test, 10,50, blank)
type = "Submit" & gt; Save & lt; / button & gt;}
and complete System, using System.Collections.Generic; System.Linq; System.Web; Namespace TestApp.Models {Public Class TestModel {Public String Text {get; set;}}} using the system
This design is by request
and / or view data ( The value is used in the
override values in actual model data This is necessary for the values posted to override. ModelState
object, and the values from ViewBag
are included in the Model; Model code in model code
For example, in which case a user posts a post, but there is an error which prevents data from being saved, the user is sent back to the form. What should be here now? Because the data was not saved, the model is still a basic value from the database, or whatever if the value of the model is preferred, then what was recorded by the user before it will be overwritten. However, if modelstate
values are used, then the user sees that form, as they submit it originally and can make the necessary modifications to resubmit. Obviously the latter option is ideal.
No comments:
Post a Comment