MVC 5 integer validation when entering a non integer - popup

I am coding a MVC 5 internet application, and have a question in regards to validating a integer value in a view.
I have a model with the following variable:
public int integerValue { get; set; }
If I enter a string value in the view for this model, I am getting an alert pop up that says:
Please enter a number.
This is not a normal validation message, and instead appears to be related to the browser.
My question is this: Is it possible to override this alert pop up to instead have the usual validation message beneath the input field?
Thanks in advance.

That will be because Mvc has output an editor with input[type=number].
If you can live without the html5 spinner controls, you can try adding
[DataType(DataType = DataType.Text)]
to the property in question.
Or you can try
#Html.EditorFor(m => m.{YourProperty}, new { htmlAttributes= new{ #type="text"}})

Related

What work is needed to pre-populate input form values using the URL/query strings?

I am creating a web portal. I have a button on the portal which redirects the user to a different website (owned by us but completely different website). I would like to pre-fill some of the input fields on a form with data passed from the portal (First name, last name and email).
I understand you can use query strings in the URL to populate fields on a form. I have tried using mywebsite.com/page/?FirstName=Jane but it is not populating the input fields. This method seems ideal but I assume there may be some work to do on the separate website to get this to work.
Both my portal and the other website is built with asp.net mvc.
Can anyone confirm if can get away with passing parameters without modifying the different website?
It's been a while since I've done mvc.
if you want to link with mywebsite.com/page/?FirstName=Jane
Assuming you have control over the page you're linking to, the page that you link to needs to
1. The model needs to have firstName in it
public class SomeModel
{
[Required]
[Display(Name = "First name")]
public string FirstName { get; set; }
}
The controller needs to populate it from that query string
public ActionResult Index(SomeModel model)
{
return View(model);
}
The view needs to use it
#model Site.Models.SomeModel
#Html.TextBoxFor(m => m.FirstName, new { #class = "form-control" })

how to avoid field disposed in a WizardPage

I have a Wizard , with two wizard Pages.
On each page I have controls, on page 2 I have some text fields,
in my controller I wanto to access them, like this:
f2.setCantSurcos(Integer.getInteger(wizard.getTxtCantsurcos()));
Where wizard method is a wrapper to page 2:
public String getTxtCantsurcos() {
return this.page2.getTxtCantsurcos();
}
The problem is that the method throw me this error:
" Widget is disposed"
I suppose this is because I'm tryin to access the widget directly:
public String getTxtCantsurcos() {
return txtCantsurcos.getText();
}
If i'm correct, I should move/copy the content of the Text field to a String attribute.
But, how to do that when the user click on Next Button ?
Best regards.
Nico
Don't try and wait for the next button to be clicked.
Use addModifyListener to add a modify listener to each text control and save the value in a string whenever the text is modified.
You can also use JFace 'data binding' for this sort of thing.
Since I cannot comment Greg's post, I post an answer:
Here is a snippet of a simple data binding example:
http://git.eclipse.org/c/platform/eclipse.platform.ui.git/plain/examples/org.eclipse.jface.examples.databinding/src/org/eclipse/jface/examples/databinding/snippets/Snippet014WizardDialog.java

Devexpress XAF. How apply autocomplete to field of string?

I have class
[DomainComponent]
[DefaultClassOptions]
public interface IEmployee
{
[XafDisplayName("Место рождения")]
String PlaceOfBirth { get; set; }
}
When the user try to fill property "PlaceOfBirth" in Web GUI, I want to help him with standart autocomplete (like http://jqueryui.com/autocomplete/ or same). Datasource of autocomplte is all value of "PlaceOfBirth" (distinct).
I already try many cases but not success. Is it possible?
PS: For now I use this advice https://documentation.devexpress.com/#Xaf/CustomDocument3116 but it not good for large collection :(
Look at DevExpress support center there are solutions available.
http://www.devexpress.com/Support/Center/Example/Details/E3325.
http://www.devexpress.com/Support/Center/Question/Details/K18561

using Ajax , dropdownlist and page validation

using Ajax I filled Country, State and city dropdownlist. On land change state is filled and on state change city is filled properly.
Then when I try to save page I face this :Invalid postback or callback argument.
Searched and found out that this is due to change in ddl.selectedvalu change from initial value that is assigned by asp.net.
Now my question is that how can I let asp.net know that the new ddl value is valid?
Thank you.
In many pages it is recommended to use EnableEventValidation="false", but I prefer not to use it.
Some say that use Render and add value to notify .Net like this:
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
ClientScript.RegisterForEventValidation("ddlLanguages ", "English");
ClientScript.RegisterForEventValidation("ddlLanguages ", "Tamil");
ClientScript.RegisterForEventValidation("ddlLanguages ", "Hindi");
base.Render(writer);
}
but how to use it? where to put it?
for a better understanding I put here a sample code including Database script :
hesab20.com/DownLoad/Ajax.zip
in this sample Javascript is used to fill drop-down list . But when click button is executed and a post back occur, error happen.
Please help if you have experience with this.
Regards.
Please run the sample code and change the drop down lists , automatically the other is filled , meaning that list item text and value is completely changed. finally click button for a post back.
you must see that error happens:
Invalid postback or callback argument. Event validation ....
and note : EnableEventValidation="true"
Thanks

ValidationMessageFor returns no information when calling modelState.AddModelStateError

I created a class that implements the IModelBinder interface. Within a method of that class I basically retrieve some values and try to validate them. If the validation fails, I add update the model state with necessary information like below:
DateTime mydate;
if (!DateTime.TryParse(convValue,out mydate))
{
bindingContext.ModelState.AddModelError("Date", "Date was crap");
}
The problem is that Html.ValidationMessageFor(m => m.Model) returns no value. I looked at the MVC source code and found out that a proper key with id "Date" can't be found in the ModelState dictionary.
Why is that ? The controller that returns the view have access to the model state and can enumerate over ModelState.Errors
Thanks,
Thomas
Is "Date" the name of the property you are validating?
The first parameter of ModelState.AddModelError should either be the name of the property you want the validation message to display for, or left as string.Empty if you only want the error to display in in the validation summary.
If you want to display an error message that isn't tied to a specific property of your viewmodel, you could call <%: Html.ValidationMessage("Date") %> in your view to display that particular message if it has been set.
Edit: just realised how old this question is. Ah well, might come in handy anyway...