How to bind two values using rivets(like some or condittion)? - rivets.js

I want to bind two values to a DOM element using rivets like user.name and user.id so that it will take user.name if it is there or user.id if user.name is empty. Can someone tell me if this is possible??

You could use a oneway binder to achieve this
http://rivetsjs.com/docs/guide/#binders
An example is at http://jsfiddle.net/jWVDu/1/
<li rv-each-person="people" rv-data-pid="person.id">
<span rv-nameorid="person.name"></span>
Basically set up a new data attribute for the id (rv-data-pid), then pass the name to the binder (rv-nameorid). the binder then checks value (name) and if it is blank uses it, otherwise it uses the data attribute which has the id.

Related

Model binding fails with Kendo MultiSelect

In the model I have long? field that I like to use Kendo MultiSelect for it. The main reason for this choice is server-side filtering. It doesn't reflect current Model's value, nor it sends any value to the server. By inspected traffic, I'm sure that it doesn't update the model's value.
#(Html.Kendo().MultiSelectFor(x => x.theField)
.Name("msname")
.MaxSelectedItems(1)
.Placeholder("")
.HighlightFirst(true)
.DataValueField("Id")
.DataTextField("Text")
.AutoBind(true)
.DataSource(ds =>
ds.Read(" ", "API").ServerFiltering(true))
.Value(new long?[] { Model.theField})
)
I can put a hidden field and update its value or multiselect's change, but there should be a better solution.
I should note that this multi select is in an editor template and is used by Kendo Grid in popup editor.
UPDATE
When using nullable types, you need to use ValuePrimitive(true)! So the end code is:
#(Html.Kendo().MultiSelectFor(x => x.theField)
.MaxSelectedItems(1)
.Placeholder("")
.HighlightFirst(true)
.DataValueField("Id")
.DataTextField("Text")
.AutoBind(true)
.DataSource(ds =>
ds.Read(" ", "API").ServerFiltering(true))
.ValuePrimitive(true)
)
The main reason for this choice is server-side filtering
You can find on their demo site that DropDownList and ComboBox also support that feature. But if you insist to use MultiSelect then lets dig some of your code.
Look Name() method will give a name for your input element e.g (input, select). When form serialized it will use our input name as form's field property. If you are using HtmlHelper that ends with "For" e.g (LabelFor, MultiSelectFor) input attribute name will be named by its binded property.
Html.Kendo().MultiSelectFor(x => x.theField)
You will have
<select name="theField"> ....
You don't have to use Name() method anymore, therefore MultiSelect value will be binded as theField property as per form serialized to server.
Now if you look to Request.Form["theField"] when you debug inside your controller, you will see what value being sent. It usually a content of joined string array if multiple items selected, so you need to change theField type to handle array of string or int instead of nullable long type.
EDIT
At last you find the way to solve your problem, this solution credit to Akbari
When using nullable types, you need to use .ValuePrimitive(true)

How to change textboxfor's input name when we are using multiple model in MVC3?

I'm using multiple model hence textboxfor's input name is different because of multiple model so parameters are always null.
I explain what I mean as you can see at below:
#Html.TextBoxFor(model => model.managers.person_name)
I have two tables which are "manager" and "mayor_assistance" and I call these models with my class. Everything is fine at this point. But when I used these models with the TextBoxFor input name is set " manager.person_name" but my table which name is manager has a person_name column hence my parameters are always null as you can see html tag at the below
<input id="managers_person_name" name="managers.person_name" type="text" value="">
Then I have to changed tex box for input name with "new" tag, I get the same result
#Html.TextBoxFor(model => model.managers.person_name,new { #name="person_name" })
So how can I change TextBoxFor input name?
Finally I have found the answer.I want to share my solution who be faced with my problem.
TextBoxFor or something like EditBoxFor etc.. their names and ids are set by properties on the Model.Because of this you get parameter always Null!
The solution is very simple just use TextBox and put your column name as string
<div class="editor-field ">
#Html.TextBox("person_name")
#Html.ValidationMessage("person_position")
</div>

MVC Html.DropDownList selected value being overriden by QueryString value

I am using a dropdownlist in my view like so:
#Html.DropDownList("ClientId", Model.AvailableClients, "-- None --")
Model.AvailableClients is an IEnumerable one of the item's Selected property is set to true. If in the query string for the page request includes "ClientId=" (as in its not set) MVC ignores my selected item. I assume this is because MVC is trying to be helpful and set the selected item automatically using the querystring, but I dont want this.
How can I prevent the querystring value from overriding my item's selected value?
If the name you give your DropDownList is already the name of an element in your model then DropDownList will automatically override the selected value with the model value. If letting ClientId determine the selected value isn't an option then the only solution I know is to rename the DropDownList with a name not included in the model.
#Html.HiddenFor(m => m.ClientId)
#Html.DropDownList("ClientIdNewName", Model.AvailableClients, "-- None --", new { onchange = "ClientId.value = this.value"})
This will keep the value in ClientId without making it the default selection value.

DropDownList MVC3

I am doing a edit operation on a record in Grid . One of the column is DropDownValue.
When I go to Edit View , depending upon this dropdownvalue , I make few fields editable and readable. And , One more point is here, I didnt select the dropdown Yet, But whatever its value selected before is the one which I should retrieve. I know I have to use jQuery .But I didnt exact Syntax to do tht.
Here is my dropdown
<div id="dvstatus">
#Html.DropDownListFor(model => model.Study.StudyStatusId, Model.StatusSelectList, new { id = "ddlStatus" })
</div>
NOT SELECTED VALUE, BUT THE VALUE WITH WHICH IT IS LOADED
My requirement is how to get the dropdown value item , when it is loaded onto .cshtml
If you're not referring to the selected value of the dropdown then just pass the value from the controller to your view using your model if you're using a strongly-typed view or pass it some other way like using ViewBag and just set the value when it's passed on view.
You can add a hidden field to save the initially loaded value. Eg
<div id="dvstatus">
#Html.HiddenFor(model => model.Study.StudyStatusId)
#Html.DropDownListFor(model => model.Study.StudyStatusId, Model.StatusSelectList, new { id = "ddlStatus" })
</div>
Then you can use java script to compare current value of the drop down and the value of the hidden field(which has the initial value).

Dynamic name generation with ASP.net MVC template

Lets say I have...
#Html.EditorFor(i => i.Date)
... and I have a template defined for items of type DateTime. In my template I have...
<input type="text" name="???" />
Given that the template only receives a DateTime instance, how can one have the template dynamically generate the name of the form field based on the original Model property name being assigned to the template?
Otherwise, upon post back, the model binder won't be able to match up the value from the template with the 'Date' property assigned to it!
Cheers, Ian.
You could in turn use a Html.TextBox instead of writing out the input tag yourself and it would handle it for you:
#Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue, new {#class = "date"})
Leave the name empty and it will automatically get populated for you. If you want to explicitly write out the HTML tags yourself though, I think that you can get the name with this:
<input type="text" name="#ViewData.ModelMetadata.DataTypeName" />

Resources