Auto-resize #Html.EditorFor (ASP.NET MVC) - html

I have #Html.EditorFor , here is code. I need it to resize on height when I enter text.
#Html.EditorFor(model => model.question, new { htmlAttributes = new { #class = "form-control", placeholder = "Вопрос" } })
How I can do this?

Related

the virtual value in Text Box Asp.net MVC

I have a text box in the Crete view in the asp.net mvc , and i want to put the virtual value 0 in this text box so when i save it will save in the database as 0 how i can do that?
enter image description here
<div class="form-group">
#Html.LabelFor(model => model.Credit, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Credit, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Credit, "", new { #class = "text-danger" })
</div>
</div>
this is the controller:
// GET: DailyTransactions/Create
public ActionResult Create()
{
var Customers = _context.Customers.ToList();
List<SelectListItem> list = new List<SelectListItem>();
foreach (var item in Customers)
{
list.Add(new SelectListItem { Text = item.Customer_Name, Value = item.Customer_Id.ToString() });
ViewBag.Customers = list;
}
return View();
}
To illustrate the idea i have in my project value in database name is credit so i want put this value as 0 i mean the virtual value 0 , so when i add some number to this credit the 0 will be increase .
so i Crete the text box and i want to put the virtual value 0 in this text box.
i hope you understand what i want.
thank you
If you want to "suggest" a value without setting it, you use placeholder:
<input id="Text1" type="text" placeholder="0"/>
If you want to set a default value, then you set that value like this:
<input id="Text1" type="text" value="0" />
If you want to bind a value to your model, you use:
// your imports / declarations
#model myModel;
#using (Html.BeginForm())
{
// other html / razor code
#Html.EditorFor(myModel => myModel.myNumber);
}
This should help, just copy and paste over your EditorFor
#Html.TextBoxFor(model => model.Credit, htmlAttributes: new { #class = "form-control" , #Value = "0", #placeholder = "0",#min="0" })
As you can see I added the html attribute in the MVC html helper and added a min so the values below 0 will not be accepted.

Textbox width in razor

how to adjust textbox width in razor code? My code below does not work:
#Html.EditorFor(model => model.PDFLink, new { htmlAttributes = new {#class = "form-control",style="width:300px" } })
Thanks.
You can create a class with the Width you want and use that class for all text box you want. Something like below.
.setWith {
max-width: 350px;
}
#Html.EditorFor(model => model.PDFLink, "", new { #class = "form-control setWith", placeholder = "If You want Any.." })
I did something like this and it works.
#Html.EditorFor(model => model.PDFLink, new { htmlAttributes = new { #class = "form-control",#style="100% !important; min-width:600px;" } })

How do I add other attributes to htmlAttributes?

The code below works, but when I try to add another attribute to htmlAttributes, it shows an error that I cannot do this.
#Html.EditorFor(model => model, new { htmlAttributes = new { #class = "form
control" } });
You are putting the next attribute outside of the htmlAttributes object by the looks of it. Instead, you want this:
#Html.EditorFor(model => model, new { htmlAttributes = new { #class = "form control", style = "background: blue" }})
You need to do something like following:
#Html.EditorFor(model => model, htmlAttributes: new { #class = "form control",
id="id"
}
})

Autofocus on EditorFor

I would like to autofocus on an editorfor in my application, but I can't seem to do that. I have successfully used autofocus on a textbox, but I would like to use an editorfor to keep my application's look universal.
Any solutions to this would be much appreciated, thank you.
My attempt:
#Html.EditorFor(model => model.Description, new { htmlAttributes = new { #class = "form-control" }, autofocus = "" })
This s because you are using EditorFor instead of something specific like TextBoxFor.
#Html.TextBoxFor(model => model.Name, new { htmlAttributes = new {
#class = "form-control" }, autofocus="autofocus"})
Or you can do that using jQuery:
<div class="editor-field focus">
#Html.EditorFor(model => model.Description)
#Html.ValidationMessageFor(model => model.Description)
</div>
$(function() {
$('.focus :input').focus();
});
Update:
As you know TextBoxFor always creates a textbox with type input, But EditorFor is a little bit smart, it renders markup based on the datatype of the property.
Using .Net Framework 4.5 and MVC 5, this works for me:
#Html.EditorFor(model => model.Description, new {
htmlAttributes = new {
#class = "form-control",
autofocus = true
}
})
You put the autofocus attribute in the wrong spot.
#Html.EditorFor(model => model.Description,
new { htmlAttributes = new { #class = "form-control" }, autofocus = "" })
Try this instead:
#Html.EditorFor(model => model.Description,
new { htmlAttributes = new { #class = "form-control", autofocus = "" } })
I've been following this thread and I may have stumbled on an answer to your question about autofocus on EditorFor - this is all Asp.Net 4.5 and MVC 5, not that it matters.
In the Scripts folder I have a jQuery script file:
$(function(){
$('.someclassname').focus();
});
I add the script name to the BundleConfig and render it in the view.
In the view I add the classname to the EditorFor <div class="col-md-10" someclassname">
I then add the type="text" autofocus="autofocus" to the EditorFor's #class. So, new{#class="form-control", type="text", autofocus="autofocus"
That's pretty much it, when the DOM loads the .someclassname field gets the cursor focus...
PS. In fact if you just do (3) it works also...

Html form adding in hidden redirect button

I'm using umbraco and letting it handle allowing my users to log in. I tried creating my own log in MVC but there is a lot of logic already built upon the old way. And I don't want to spend the time having to re-build it.
There for, I was wondering if there was anyway I could add in a hidden field that would allow the user to be redirected to my home page when the log in using the following form code:
#using (Html.BeginUmbracoForm<UmbLoginController>("HandleLogin"))
{
<legend>Login</legend>
#Html.ValidationSummary("loginModel", true)
#Html.TextBoxFor(m => loginModel.Username, new { #class = "form-control", #placeholder = "Username" })
#Html.ValidationMessageFor(m => loginModel.Username, "", new { #class = "alert-danger", #role = "alert" })
<br />
#Html.PasswordFor(m => loginModel.Password, new { #class = "form-control", #placeholder = "Password" })
#Html.ValidationMessageFor(m => loginModel.Password, "", new { #class = "alert-danger", #role = "alert" })
<br />
<button class="btn btn-default">Login</button>
}
You should be able to set the loginModel.RedirectUrl at the point where you're creating it, and then add the following to your form:
#Html.HiddenFor(m => loginModel.RedirectUrl)