I use <input type="file" id="fileId" name="fileId"/> and
<% = Html.TextBoxFor (x => x.FileName, new {# class = "className", maxlength = 255, id = "fileName"})%> in my mvc project. I want to save in a text box the file name selected in the INPUT element. How can I do this?
You need to use javascript to achieve this. Here's an example with jquery:
$(function() {
$('#fileId').change(function() {
// When the user selects a file, read the selected filename
// and set it to the textbox
var filename = $(this).val();
$('#fileName').val(filename);
});
});
Related
mvc option with html content
how to write/render
<select>
<option><span style="">Hi</span></option>
<option><span style="">Hi1</span></option>
<option><span style="">Hi2</span></option>
</select>
using #html.DropDownList()
--------Update-------------
i want to use html formatted text in option
when i passed it from modal, it was showing in dropdown list
<span style="">Hi</span>
<span style="">Hi1</span>
<span style="">Hi2</span>
here the span style="" is html it is not string, and the list is coming from controller
Use SelectItem in the code, insert options in that list, provide another property for selected item on Post using passing it to the view and use #Html.DropDownListFor(x => x.SelectItem) a simple example can be
#Html.DropDownListFor(model => model.Package.State, new SelectList(
new List<Object>{
new { value = 0 , text = "Red" },
new { value = 1 , text = "Blue" },
new { value = 2 , text = "Green"}
},
"value",
"text",
2))
I want to create a textbox to search for values inside a grid. When I type a text on the textbox, the grid will be updated automatically showing values corresponding to what I have typed without any need for pressing button or enter. How can I do that?
This is my three textboxes in Index.cshtml file:
<div class="col-md-3">
<div class="efield awe-il">#Html.Awe().TextBox("txtVINnumber").Placeholder("Find VIN number").CssClass("searchtxt") </div>
</div>
<div class="col-md-3">
<div class="efield awe-il">#Html.Awe().TextBox("txtEngineVINnumber").Placeholder("Find Engine VIN number").CssClass("searchtxt") </div>
</div>
<div class="col-md-3">
<div class="efield awe-il">#Html.Awe().TextBox("txtGoukinumber").Placeholder("Find Gouki number").CssClass("searchtxt") </div>
</div>
And this is my action method:
public ActionResult GetItems(GridParams g, int? atdid, bool? showDeleted, string vinNumber, string engineVinNumber, string goukiNumber)
{
vinNumber = (vinNumber ?? "").ToLower();
engineVinNumber = (engineVinNumber ?? "").ToLower();
goukiNumber = (goukiNumber ?? "").ToLower();
IQueryable<VehicleRecord> query = _context.VehicleRecords.Where(o => o.VinNumber.ToLower().Contains(vinNumber) && o.EngineVin.ToLower().Contains(engineVinNumber) && o.GoukiNumber.ToLower().Contains(goukiNumber)).AsQueryable().Include(p => p.Spot).Include(l => l.VehicleLocation);
int id = Convert.ToInt32(g.Key);
if (showDeleted.HasValue)
{
query = query.Where(p => p.IsDeleted == showDeleted.Value);
}
if (!vinNumber.IsEmpty())
{
query = query.Where(o => o.VinNumber.Equals(vinNumber));
}
return Json(new GridModelBuilder<VehicleRecord>(query, g)
{
Key = "Id",// needed for Entity Framework | nesting | tree
Map = o => new
{
o.Id,
o.VinNumber,
o.EngineVin,
o.GoukiNumber,
Position = string.Format("{0} - spot # {1}", o.VehicleLocation.Name, o.Spot.SpotNumber),
o.IsDeleted
}
,
GetItem =
() => query.FirstOrDefault(x => x.Id == id)
}
.Build());
}
}
Edit: This is what i have done so far
I think you can achieve this with ajax and JQuery.
Steps
Register the textboxes with changed events. For this step refer JQuery code to get values changed in text box.
In current example I have written alert for change in values.
On change event collect the values from textbox and sent to across to your controller method the same method your are using on click button.
Here you need to rplace the alert with ajax call.
For the step one just include id like textbox1, textbox2 and textbox 3 and hook up with change event
$('#elementId').change(function(){
alert("changed");
//Call here ajax code to call the controller method which will reload the grid
});
previousVal = "";
function InputChangeListener()
{
if($('#elementId').val()
!= previousVal)
{
previousVal = $('#elementId').val();
$('#elementId').change();
}
}
setInterval(InputChangeListener, 500);
$('#elementId').val(3);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<input type="text" id="elementId" />
.
Now Instead of alert put ajax call over there
$.ajax({
type: "POST",//Your type of call get or Post
url: "http: ~/GetItems", //URL to application
content: "application/json; charset=utf-8",
data: dataRequest,//All the parameters
success: function(d) {
//Update your grid
},
error: function (xhr, textStatus, errorThrown) {
// TODO: Show error
}
});
I have to pass the dropdown selected value in knockout function. I have crated the razor syntax for that:
<div class="row">
#Html.LabelFor(m => m.FilterByType, htmlAttributes: new { #class = "label" })
#Html.DropDownListFor(m => m.FilterByType, new SelectList(Model.aspNetUser, "FilterByType", "FilterByName"), new { #class = "selectBox", #id = "aspnetUsersType", #data_bind = "event: {change: getData(0,'','',size,index,'')}" })
<input type="hidden" id="filterByType" name="filterByType" value="">
</div>
Following is my knockout function:
self.getData = function (filterbytype, fromdaterange, todaterange, pageSize, page, searchText) {"some task"}
How do I pass the selected value in the getData? Right now I am passing 0 as a filterbytype.
The objects can be passed from server to client using JSON, and binded to a property of type observableArray. Then bind this property to a html element using foreach binding. You can see an example here:
http://knockoutjs.com/documentation/foreach-binding.html
Suppose are using this following code in ur HTML
<select data-bind="options: operations, value: self.selectedOperation">
Make sure that You must be using these variables as ko.observable() type
so ur JS code will look like this
var self = this;
self.selectedOperation = ko.observable("Option1"); // with self u can use more variables and here selectedOperation will be containing the choosen value you want and you can pass it wherever u want
var operations = ko.observableArray(["Option1", "Option2", "option3", "Opt4"]); // dropdown List
Now its up to you how you are going to use that variable..
In my html.beginform I have this checkbox that I want to persist. I'm not sure how to add it to the below ActionLink...or if it is even possible. I thought of using a session variable. If anyone has an example showing how to set a checkbox value in a session variable that would be awesome...or if there is a way to pass it in the ActionLink...that would be cool too.
cheers and thanks in advance
#Html.CheckBoxFor(model=>model.wccrSelection.SendDemand)
#Html.ActionLink("Order",
"Display", new {Model.wccrSelection.WccrId,sortOrder = ViewBag.NameSortParm })
You could use a <form> instead of a link. This way the value of the checkbox will be automatically sent to the controller and you don't need to use any javascript:
#using (Html.BeginForm("Display", null, new { id = Model.wccrSelection.WccrId, sortOrder = ViewBag.NameSortParm }))
{
#Html.CheckBoxFor(model=>model.wccrSelection.SendDemand)
<button type="submit">Order</button>
}
You can use jQuery to add additional parameter for passing checkbox state to your action method as shown below:
Eg.:
#Html.CheckBoxFor(model=>model.wccrSelection.SendDemand, new {#class = "SendDemand"})
#Html.ActionLink("Order",
"Display", new {Model.wccrSelection.WccrId,sortOrder = ViewBag.NameSortParm }, new { #class = "DisplayOrder" })
<script src="#Url.Content("~/Scripts/jquery-1.7.1.min.js")" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
(".DisplayOrder").on.("click",function(){
var href = $(this).attr("href");
if ($(".SendDemand").is(':checked')) {
href = href + "&CheckBoxState=true";
}
else {
href = href + "&CheckBoxState=false";
}
$(this).attr("href", href);
});
});
</script>
I am working on Asp.net MVC.
i am using regular html
<label for="txtbox1" > User Name</label>
<input type="text" id="txtbox1" name="text" class="water"/>
I want to display Cursor before text and Text both when page load. When user start writting in text box at that time "User Name" removed.
You need to use javascript for this. If you are using jquery you could:
$(function() {
$('#txtbox1').focus();
});
And if you want to do this with plain javascript:
window.onload = function() {
var textbox = document.getElementById('txtbox1');
if (textbox != null) {
textbox.focus();
}
};
UPDATE:
If you want the text to disappear when the user starts typing you may try the following:
$('#txtbox1').keypress(function() {
if (!$(this).data('dirty')) {
$(this).val('').data('dirty', true);
}
});
Live demo.