Send int? parameter to command UWP - binding

I have a list ListView type. Current selected index of list is binded to int? property. I want to send selected index as parameter to Execute method of ICommand interface like that:
CommandParameter="{x:Bind ViewModel.SelectedIndex}"
When i debug execute method, debugger is tell that parameter is null. But selected index is have value. How can i send binded int? parameter to this method? Thanks!

CommandParameter="{x:Bind ViewModel.SelectedIndex, Mode=OneWay}"
use mode = oneway so that u can get updated value of selected index everytime it changes. Also make sure your ViewModel implements INotifyPropertyChanged for this to work properly.
https://learn.microsoft.com/en-us/uwp/api/windows.ui.xaml.data.inotifypropertychanged

Related

Is this by design in MVC Model Binding?

A simple scenario that I've never seen before, but a colleague has just hit - MVC3
Create an action method MyAction(int myProperty = 0)
Create a model that has a property MyProperty
Pass an instance of this model to a strongly typed view, but set the property to 10 in code (don't use the query string parameter!)
In the view, Html.TextBoxFor(x => x.MyProperty)
This should render 10 in the text box.
Now call the action method MyAction?myProperty=8
Shouldn't this still render 10 in the text box?
I see that I can override the property discovered by the expression and assume this is because they are the same name (Query String parameter and model property). Eveything is then in the ViewData but one overrides the other.
Is this by design?
This is by design - ModelState is the highest priority value-provider for model properties, higher than even model itself. Without query string parameter, ModelState does not contain value for MyProperty, so framework uses model value.
You can use ModelState.Remove("MyProperty") to ensure using model value
If you look at the source code for Html.TextBoxFor you will see that if a value exists in ModelState then it will always use that value before any other.
string attemptedValue = (string)htmlHelper.GetModelStateValue(fullName, typeof(string));
tagBuilder.MergeAttribute("value", attemptedValue ?? ((useViewData) ? htmlHelper.EvalString(fullName, format) : valueParameter), isExplicitValue);
If the value is in ModelState, then it doesn't matter what you set in code.

How to retrieve a property value from modelstate

How can I retrieve a property value from modelstate in httppost action.
Below is the code, I used to retrive the hidden id field from modelstate. But is it not possible to have strongly typed version to get value. Like, if property name is modified, prompting a compile time error.
Could anyone please explain difference between "AttemptedValue" and "RawValue".
ModelState state;
if (ModelState.TryGetValue("id", out state))
{
string value = state.Value.AttemptedValue.ToString();
}
Attempted value is used by the framework and it contains concatenated list of values. In my case, since it is id field, I am going ahead with attempted value. Below link has more information on this.
http://forums.asp.net/t/1571473.aspx/1?MVC+2+Custom+ModelBinder+and+storing+the+attempted+value+for+the+view
you can iterate the ModelStateDictionary object and through the keys(property name) on the dictionary get the value of the desired property or you can do something like ModelState["PropertyName"].Value

ASP.NET MVC - Redirect/Post on Dropdown list change. Add to Route Values?

I have a dropdown list bound to some Viewdata. When a user changes the value, I'd like to essentially add the value of the dropdown to the routevalue dictionary so it's a parameter but keep the existing routevalues in place.
Right now I've got it so changing the value forces the page to post as follows:
#Html.DropDownList("Regions", Nothing, New With {.onchange = "this.form.submit();"})
This obviously isn't what I want as I've had to repeat the code in the post section in the controller and I lose the other routevalues.
Is there anyway I can amend the .onchange part so it sends the value of the dropdown through to the routevalues and refreshes the page?
I ended up resolving this by manually constructing the parameter string using Javascript and using window.location.

How do I access a variable value assigned to an HTML.Hidden variable in a MVC Controller Action Method

I am writing my first ASP.Net webpage and using MVC.
I have a string that I am building in a partial view with a grid control (DevExpress MVCxGridView). In my partial view I am using a HTML.Hidden helper as shown below.
' Create a hidden variable to pass back a comma-delimited string
Response.Write(Html.Hidden( "exclusionList", Model.ExclusionList))
The value of of this hidden element is assigned in client side javaScript:
exclusionListElement = document.getElementById("exclusionList");
// ...
exclusionString = getExclusionString();
exclusionListElement.value = exclusionString;
This seems to work without problem.
In my controller action method:
<AcceptVerbs( HttpVerbs.Post )> _
Public Function MyPartialCallback(updatedItemList As myModel) As ActionResult
Dim myData As myModel = GetMyModel()
Return PartialView( "MyPartial", myModel.myList )
End Function
The updatedItemList parameter is always nothing and exclusion list exists no where in the Request.Forms.
My questions are:
What is the correct way to use Html.Hidden so that I can access data in a MVC Controller Action method.
Is adding "cargo" variables to Request.Form the best and only way to send data back to a server side MVC Controller Action method? It just seems like twine and duct-tape approach. Is there a more structured approach?
If you need to get the exclusionList variable back, you just need to add a property to your view model that matches that name exactly. Make sure it is of the correct type (string it looks like in this case) and then it should auto populate that property in the view model for you.
And yes, there is no need for the Response.Write call. Instead just use the Html.HiddenFor(...) helper in your view.
Look at the generated HTML. Note down the name attribute of the hidden field. Use this name as action parameter name:
Public Function MyPartialCallback(exclusionList As string)

Add empty value to a DropDownList in ASP.net MVC

I'm building a data entry interface and have successfully bound the columns that have reference tables for their data using DropDownList so the user selects from the pre-configured values.
My problem now is that I don't want the first value to be selected by default, I need to force the user to select a value from the list to avoid errors where they didn't pick that field and by default a value was assigned.
Is there a more elegant way of doing this than to add code to include an empty value at the top of the list after I get it from the database and before i pass it to the SelectList constructor in my controller class?
The Html helper function takes a 'first empty value' parameter as the third argument.
<%=Html.DropDownList("name",dataSource,"-please select item-")%>
You can also use this way:
dropdownlist.DataTextField = ds.Tables[0].Columns[0].Caption;
dropdownlist.DataValueField = ds.Tables[0].Columns[1].Caption;
dropdownlist.DataSource = ds;
dropdownlist.DataBind();
dropdownlist.Items.Insert(0, new ListItem("Select ...", string.Empty));

Resources