MVC4: request[field] returning System.Web.Mvc.SelectListItem unexpectedly - asp.net-mvc-4

I have an Index page which shows a list of data. The list can be filtered by selection from a dropdown list. This works okay. The first item in the dropdown has a blank value and the text "(all users)." So, this does what you expect--it shows all values in the list if you don't select an item to filter by. The razor for this is pretty simple:
#using (Html.BeginForm())
{
#Html.Label("userName", "View user:")
#Html.DropDownList("userName", ViewBag.UserName as SelectList, new { onchange = "this.form.submit()" })
}
In addition to viewing the list of data on the page, I offer a text file download of the currently filtered items. The razor for this is also pretty simple:
#using (Html.BeginForm("Download", "Checkout"))
{
#Html.Hidden("userName", Request["userName"])
<input type="submit" value="Download Metadata from Checked Out Files" />
}
This works right IF there's something selected in the filter dropdown. But if nothing is selected, then Request["userName"] returns "System.Web.Mvc.SelectListItem". I want it simply to be blank as the dropdown list appears when nothing is selected. What happens is that the value System.Web.Mvc.SelectListItem is passed to my download action and treated as a filter value.
So, the question is how to make it so a blank selection in my filter dropdown is not mistaken for "System.Web.Mvc.SelectListItem."

Try changing your #Html.DropDownList to this overload, string.Empty gives you the default blank option in your dropdown
#Html.DropDownList("userName", ViewBag.UserName as SelectList, string.Empty, new { onchange = "this.form.submit()" })
I hope I didn't misunderstand your question!

Related

Placeholder for Lookup in TPC (The Portal Connector)

I have a requirement to provide placeholder for all controls in my TPC form. I wanted to know if there is a way to do so. I have tried placing placeholder in Template like:
<input id='#Html.GetUniqueId(Model.Name)_input' data-tpc-role="lookup-input" name="#Model.MetaField.FieldName" data-tpc-default-value="#Model.GetLookupValue()" data-tpc-value="#Model.GetLookupValue()"
placeholder ="myplaceholdertext" type="text" #MvcHtmlString.Create(#Model.ValidationAttributes) data-tpc-custom-validation="lookup"/>
and through script
$(document).on("tpc:ready", function(){
var picklistName = "mypicklistname";
//Set Text to Placeholder Value
tpc.forms[0][picklistName].get_kendoInput().text("Please select an option.");
});
None of these work.
Let me know if this is the correct forum to ask TPC related questions
TIA
You have the kendo control but the value you want to set is the first item in its dataSource. Don't forget to refresh the control to make it show:
$(document).on("tpc:ready", function(){
var picklist = tpc.forms[0]["mypicklistname"];
//Set Text to Placeholder Value
picklist.get_kendoInput().dataSource.data()[0].Label = "Please select an option.";
picklist.get_kendoInput().refresh();
});

$_POST[] Value disappears in admin- but works in view-module

I have a problem here which i am working quite a while but do not have any ideas left for a solution. I will try to make it short:
I use the eColumns extension for the admin and the view module in yii. I added a combobox to select views which are saved in the database so that users can build their own selection of select columns for each table, however:
In eColumns i added a button to delete an already created selection and an input field to give the selection a name
CHtml::button('', array('type' => 'submit','name' => 'btn_delete','value' => 'Ansicht löschen', 'onclick' => '$("#'.$this->getId().'").dialog("close");', 'style' => 'align: left', 'confirm'=>'Sind sie sicher das sie diese Ansicht löschen möchten?'));
CHtml::textField('input_name', substr($this->selectedView,strpos($this->selectedView,"##")+2), array('size'=>30,'maxlength'=>200));
If i click the button in the view-module everything works as expected. $_POST is filled with "input_name" and "btn_delete". However if i this same code included in the admin-module only input_name is filled - btn_delete is simply not set if i click the button.
Anybody can i give me any hint what i can check?
Thanks in advance! :)
If you are using jQuery serialize() to collect form elements in your admin-module, jQuery serialize() will not serialize any submit button value.
See also serialize example, hope this will help. :)
=== links to jsfiddle must be accompanied by code.. ===
html
<form>
<input type="text" name="text" value="text-value">
<input type="submit" name="submit-btn" value="button-value">
</form>
<span name="result-serialize"></span>
jQuery
var serializeString = $("form").serialize();
$("span[name=result-serialize]").text(serializeString);

Read and Write Values from Jquery Datatable

I am generating a html table dynamically and after that using datatable 1.9.4 plugin to enhance my html table's features. My table looks like below
the "x" are checkboxes, I am trying to select checkboxes with respect to a particular row and column combination. I have values in pairs like (row1,col3) then I need to select that particular cell. I tried reading checkbox text from header row and selecting corresponding checkbox but no luck.
How should I proceed with this. I know there are some methods from dataTable but not very well aware of those.
You lack to tell how exactly you generate those checkboxes. So the following is just how what I would do it. There isnt really difference if the checkboxes is inside a dataTable or anything else.
Example of creating a lot of checkboxes inside a dataTable, all with unique ID's :
for (var row=1;row<=10;row++) {
dataTable.fnAddData([
'<input type="checkbox" id="check_'+row+'_1">',
'<input type="checkbox" id="check_'+row+'_2">',
'<input type="checkbox" id="check_'+row+'_3">',
'<input type="checkbox" id="check_'+row+'_4">',
]);
}
A shorthand function for retrieving the right checkbox, based on row, col pairs :
function dtCheckBox(row, col) {
return document.getElementById('check_'+row+'_'+col);
}
Some examples to show the concept is working :
//setter examples, check 20 random checkboxes
for (var i=0;i<20;i++) {
var row=Math.floor(Math.random()*10)+1,
col=Math.floor(Math.random()*4)+1;
dtCheckBox(row, col).checked=true;
}
//getter example, console out the checked status of all checkboxes
for (col=1;col<=4;col++) {
for (row=1;row<=10;row++) {
console.log(dtCheckBox(row, col).checked);
}
}
See the above in action in this fiddle -> http://jsfiddle.net/EpLA7/

Passing a GET parameter to ActionLink in ASP.NET

Sorry but I am new to C# and ASP.NET and I saw alot of posts about this problem but I quite didn't get it. I am trying to understand how to pass a GET parameter to an action thru HTML.ActionLink:
here is the the URL:
http://localhost:36896/Movies/SearchIndex?searchString=the
and my CSHTML page should look like this:
<input type="Text" id="searchString" name="searchString" />
#Html.ActionLink("Search Existing", "SearchIndex", new { searchString = "the"})
this hard coded parameter "the" is actually working, but how can I select the input element with id=searchString, with something like document.getElementById("searchString").value
Thanks,
If the value you want to send as GET parameter is not known on the server you cannot use the Html.ActionLink helper to add it. You need to use javascript to manipulate the existing link and append the parameter.
It looks like you have an input field that contains a search string and you want to send the value entered in this field to the server. A better way to handle this scenario is to use an HTML form with method="GET" instead of an ActionLink. This way you don't need to use any javascript - it's part of the HTML specification:
#using (Html.BeginForm("SearchIndex", "Movies", FormMethod.Get))
{
#Html.EditorFor(x => x.SearchString)
<button type="submit">Search</button>
}
Now when you click on the Search button the value entered in the SearchString field will automatically be sent to the SearchIndex action:
http://localhost:36896/Movies/SearchIndex?searchString=the
But if you absolutely insist on using an ActionLink you will have to write javascript to manipulate the href of the existing link when this link is clicked in order to append the value to the url. It's an approach I wouldn't recommend though because the HTML specification already provides you this functionality throughout HTML forms.
This makes the #Html.EditorFor refer to the Title field of the object, kinda in a random way but it works!
#using (Html.BeginForm ("SearchIndex", "Movies", FormMethod.Get))
{
#Html.EditorFor( x => x.ElementAt(0).Title)
<button type="submit">Search</button>
}
Still couldn't pass input parameter to the URL in the GET.
EDIT:
FINAL SOLUTION:
#Html.TextBox("SearchString")
<button type="submit">Filter</button>
and on the controller side, switch the input parameter. Basically it will automatically recognize the passed parameter.
public ActionResult SearchIndex(string searchString)
{
...
}

How to get Jquery UI tab to be blank

I have a JQuery UI tab dialog that is the detail of a Master-Detail interface. When someone selects an element in the master, the tabs all get their href's populated with URLs giving details of that selected item.
For example, see
http://www.trirand.com/blog/jqgrid/jqgrid.html and browse to Advanced->Master Detail.
But instead of updating a second grid, I'm updating the links of a jquery-ui tabs element like so:
var urls = {
0 : "/url1",
1 : "/url2",
};
jqgrid(....
onSelectRow: function(location_id) {
for (url in urls){
$('#tabs').tabs('url', url , urls[url]+location_id );
}
var selectedTab = $('#tabs').tabs("option", "selected");
$('#tabs').tabs('load', selectedTab);
}
);
$(#tabs.tabs({});
With html like:
<div id="tabs">
<ul>
<li><a id="URL1" href="blank.html">Info</a></li>
<li><a id="URL2" href="blank.html">History</a></li>
</ul>
</div>
I shouldn't have to use a blank.html dummy link. Is there something I can do (when I don't have anything selected in the master) that doesn't cause my tabs to cause a fetch and instead just be empty?
If you set the tab to be blank in your coding nothing will appear in it (obviously), but if you need to empty it on page load use this:
$(document).ready(function() {
$('#divID').empty();
});