Hi I am trying to convert the following HTML input into razor html.textbox helper using vb syntax
<input type="text" name="q" data-autocomplete-source="#Url.Action("QuickSearchTransactionNumber", "Home")" class="form-control" id="TransactionNumber" placeholder="Transaction Number">
Any help would be great
You can add custom attributes to HTML elements created by HTML helpers by defining a new dictionary like so:
#Html.TextBox("q", Request("q"),
New Dictionary(Of String, Object) From
{
{ "data-autocomplete-source", Url.Action("QuickSearchTransactionNumber", "Home")},
{"class", "form-control"}, {"id", "TransactionNumber"},
{"placeholder", "Transaction Number"}
})
This outputs the following HTML:
<input class="form-control" data-autocomplete-source="/Home/QuickSearchTransactionNumber" id="TransactionNumber" name="q" placeholder="Transaction Number" type="text" value="" />
Documentation for this overload: http://msdn.microsoft.com/en-us/library/dd505258(v=vs.108).aspx
If you want to use a LabelFor in your view (that's strongly typed to a model) you can do this:
#Html.LabelFor(Function(model) model.BranchNumber, New With { .class = "sr-only" })
You also have to annotate your data model with a Display attribute like so:
Imports System.ComponentModel.DataAnnotations
Public Class Bank
Private _branchNumber As String
<Display(Name:="Branch Number")>
Public Property BranchNumber() As String
Get
Return _branchNumber
End Get
Set(ByVal value As String)
_branchNumber = value
End Set
End Property
End Class
Related
I have a public class like this:
namespace MyProgram.Common
{
public static class UIStrings
{
public const string Title = "Hellow World"
public const string SubTitle = "This is another string. Please click on 'Here'"
}
}
And then, on my Index.csthml I have the following code:
<label id="title" for="MyTitle">#Myprogram.Common.UiStrings.Title </label>
<label id="title" for="SubTitle">#Myprogram.Common.UiStrings.SubTitle </label>
The title renders fine but the link that I defined in the Subtitle doesn't render as a link but as the string itself.
Is there a way this can be done? I want to avoid to hardcode the strings in the cshtml file...
User Html.Raw
<label id="title" for="MyTitle">#Myprogram.Common.UiStrings.Title </label>
<label id="title" for="SubTitle">#Html.Raw(Myprogram.Common.UiStrings.SubTitle) </label>
What you are looking for is the HTML decode function. MSDN
This will force the browser to interpret the output as HTML rather than as a string.
Hope this helps!
I am trying to understand how web-services work and I think I need some help with my controller. For example, I am trying to add a user into my data base ... This is what I have:
public static Result addUser(){
DynamicForm form = Form.form().bindFromRequest();
String url = "http://my-url-qqq";
WSResponse response;
WSRequestHolder holder = WS.url(url);
holder.setHeader("Cookie", "sessionid="+ session("sessionid"));
Map<String,String> anyData = new HashMap();
JsonNode content = response.asJson();
// how can i put all this things togeter
//to put the elements from my form in
//my database ... ?
//and what is the role of all the pieces ?
return ok(index.render("Bello! Now you can log in!"));
}
And I have this model:
#Entity
public class registerForm extends Model {
//for registration
#Id
public String Id;
public String username;
public String first_name;
public String last_name;
public String password1;
public String re_password1;
....
}
routes:
GET /register controllers.Application.register()
POST /register controllers.Application.addUser()
and my html form:
<form action="#routes.Application.addUser()" method="POST">
<div class="col-md-offset-1 col-md-4">
<h3><b>Register : </b></h3>
<br>
Username :
<input type="input" name="username" class="form-control"><br>
First Name :
<input type="input" name="first_name" class="form-control"><br>
Last Name :
<input type="input" name="last_name" class="form-control"><br>
Email :
<input type="input" name="email" class="form-control"><br>
Password:
<input type="password" name="password" class="form-control"><br>
Repeat Password :
<input type="password" name="re_password" class="form-control"><br>
<input type="submit" class="btn">
<br><br><br><br>
<h2> ^_^ : have fun .</h2>
</div>
</form>
Can anyone explain/translate how to connect this things ?
I'll appreciate any kind of example ...
First dont use DynamicForm when your form has same structure like your entity class means DynamicForm are used when For ex. if you want to search user from database then your form will have only one field in that case you can use DynamicForm where you can search from predefined entity field.If your form have same field like your entity fields
Second I think you misunderstood entity i.e. entity is a POJO(Plane Old Java Object) your class represent a table in database and your entity name is registrationforn and I think thats not look good you should name your entity like User or Member.This is completely optional for you but it gives better understanding
To save data do
public static Result addUser(){
registerForm user = Form.form(registerForm.class).bindFromRequest().get;
user.save(); //and the data is saved
return ok(index.render("Hello! Now you can log in!"));
}
And delete, find entity etc check Play Ebean Sample Application.
For example:
Model
public class Person
{
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
Editor Template for Person (PersonEditor.cshtml):
#model MvcApplication1.Models.Person
#Html.HiddenFor(x=>x.ID)
<label>First Name</label>
#Html.TextBoxFor(x=>x.FirstName)
<label>Last Name</label>
#Html.TextBoxFor(x=>x.LastName)
<br />
On my main page, I want to be able to do the following:
#model IList<MvcApplication1.Models.Person>
#using (Html.BeginForm())
{
#Html.EditorFor(x=>x,"PersonEditor")
}
And have all the elements in the form, generate the proper names automatically; instead of having to loop through the collection as I am doing now:
#using (Html.BeginForm())
{
for (int i = 0; i < Model.Count; i++)
{
#Html.EditorFor(x=>Model[i],"PersonEditor")
}
}
The form elements must contain the following format:
<input name="[0].ID" type="text" value="Some ID" />
<input name="[0].FirstName" type="text" value="Some value" />
<input name="[1].ID" type="text" value="Some x" />
<input name="[1].FirstName" type="text" value="Some y" />
And so on...
Because in my controller, I expect to receive an IList<Person> when the form posts pack.
Can I eliminate that for loop completely?
EDIT
Right now, when I simply do #Html.EditorFor(x=>x) (without the loop, in other words), I get this exception:
The model item passed into the dictionary is of type
'MvcApplication1.Models.Person[]', but this dictionary requires a
model item of type 'MvcApplication1.Models.Person'.
You should be able to use the same template for both IEnumerable<T> and T. The Templating is smart enough to enumerate the IEnumerable, but you will need to rename the editor template to match the type name. Then you should be able to use
#model IList<MvcApplication1.Models.Person>
#using (Html.BeginForm())
{
#Html.EditorForModel()
}
Unfortunately, it looks like a template named anything other than the type name will throw an exception
The model item passed into the dictionary is of type
'MvcApplication1.Models.Person[]', but this dictionary requires a
model item of type 'MvcApplication1.Models.Person'
I have a ASP.NET Razor view which binds to string. Its very simple:
#model string
#{
Layout = "~/Views/Shared/_Layout.cshtml";
}
Hello, #Model
#using(Html.BeginForm())
{
<fieldset>
<label for="name" style="color: whitesmoke">Name:</label>
<input type="text" id="name"/>
<br/>
<input type="submit" value="Submit"/>
</fieldset>
}
And a simple controller:
[HttpGet]
public ActionResult Index()
{
object model = "foo";
return View(model);
}
private string name;
[HttpPost]
public ActionResult Index(string name)
{
return View();
}
When I push the submit button, the Index Post action result triggers, but the 'string name' parameter is null. Isn't Razor smart enough to automatically bind this property to my controller from the view because the input id matches the name of the param on the controller? If not, how do I bind this? I know with a model with properties I can use Html.HiddenFor(m => m.Foo), but since there's no properties, I don't know how to call this method properly.. I can set it properly calling Html.Hidden("name","foo"), but I don't know how to pass a the value here. I know I can use jquery call such as:
#Html.Hidden("name", "$('input[id=name]').val())");
This literally sends the jquery string to the controller as the value... I'm not sure what to do at this point. Thanks!
It is smart enough to bind the property, just give your input a name which matches with the action parameter:
<input type="text" id="name" name="name" />
I have a form that has a hidden field wich stores a object. This object is a RoutesValues (I want to store a reference because when I process the form I want to redirect to a route). The action that processes the form is:
public ActionResult Añadir(string userName, string codigoArticulo, string resultAction, string resultController, object resultRouteValues, int cantidad)
{
processForm(codigoArticulo, cantidad);
if (!ModelState.IsValid)
TempData["Error"] = #ErrorStrings.CantidadMayorQue0;
if (!string.IsNullOrWhiteSpace(resultAction) && !string.IsNullOrWhiteSpace(resultController))
return RedirectToAction(resultAction, resultController, resultRouteValues);
return RedirectToAction("Index", "Busqueda", new {Area = ""});
}
and my form is:
#using (Html.BeginForm("Añadir", "Carrito", FormMethod.Get, new { #class = "afegidorCarrito" }))
{
<fieldset>
<input type="hidden" name="codigoArticulo" value="#Model.CodiArticle" />
<input type="hidden" name="resultController" value="#Model.Controller" />
<input type="hidden" name="resultAction" value="#Model.Action" />
<input type="hidden" name="resultRouteValues" value="#Model.RouteValues" />
<input type="text" name="cantidad" value="1" class="anadirCantidad" />
<input type="submit" />
</fieldset>
}
the problem I have is that resultRouteValues gets passed as a string instead of an object. Is there any way to fix this?
Thanks.
No, there is no easy way if RouteValues is a complex object. You will have to serialize the object into some text representation into this hidden field and then deserialize it back in your controller action. You may take a look at MvcContrib's Html.Serialize helper.