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!
Related
I have the following model:
public class ReadModel
{
public string Name { get;set; }
}
public class EditViewModel
{
public ReadModel Data { get;set;}
}
I then populate this in the controller and pass this to the view:
#model EditViewModel
<form asp-action="Edit">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Data.Name" class="control-label"></label>
<input asp-for="Data.Name" class="form-control" />
<span asp-validation-for="Data.Name" class="text-danger"></span>
</div>
</div>
</form>
However when the page displays there seems to be now errors but the label is blank and the input is also blank.
Can anyone help me hear?
Update
I can use the old method of
#Html.EditorFor(m => m.Data.Name)
and i get a input textbox along with the data in the textbox
Update
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
Answer:
The issue was that I was inside an area and had not copied over the _ViewImports which has #addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers.
I have next form:
<form asp-controller="Chat" asp-action="AddFile" method="post" asp-route-chatId="#Model.ChatId" enctype="multipart/form-data">
<textarea id="messageInput" class="textInput" style="width: 80vh" name="messageInput"></textarea>
<div>
<input type="submit" id="sendButton" value="Send Message" />
<input type="file" class="inputfile " id="File" name="File" value="File"/>
<label for="File">Choose a file</label>
</div>
</form>
ViewModel
public class ChatFileViewModel
{
public long ChatId { get; set; }
public string messageInput { get; set; }
public IFormFile File { get; set; }
}
and post method:
[HttpPost]
public void AddFile([FromBody] ChatFileViewModel chatFile)
{ ... }
The issue is - every time i press submit it transfers ChatId correctly, while messageInput and File are null. I have no idea what it is, because i have exactly the same construction working correctly in the other part of my app.
Using [FromBody] To force Web API to read a simple type from the request body, but your object is complex contain string and int can not treat as simple type.
Remove FormBody, I reproduce and it worked
More about FormBody at https://learn.microsoft.com/en-us/aspnet/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api
You should remove the [FromBody] attribute, since your request data is in the form. [FromBody] is usually used to deal with data in the request body like JSON and XML
[HttpPost]
public void AddFile(ChatFileViewModel chatFile)
{ ... }
For more details, you can refer to Model binding.
if you need of want to specify binding then you can use [FromForm] because you correctly setup for into multipart/form-data but it depends on your other actions
public IActionResult AddFile([FromForm]ChatFileViewModel model)
if you use asp- tag helpers you can use them for controls as well
<form asp-controller="Home" asp-action="AddFile" method="post" asp-route-chatId="#Model.ChatId" enctype="multipart/form-data">
<textarea asp-for="messageInput" class="textInput" style="width: 80vh" name="messageInput"></textarea>
<div>
<input type="submit" id="sendButton" value="Send Message" />
<input type="file" class="inputfile " asp-for="File" />
<label asp-for="File">Choose a file</label>
</div>
</form>
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.
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
I have two links say for eg. English and Spanish. When user clicks on english link it should set language to "English" in session.
The following is my aspx code
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<script type="text/javascript">
function SetLanguageSpanish() {
document.getElementById("Home_Language").value = 'Spanish';
document.getElementById("frmHome").submit();
}
function SetLanguageEnglish() {
/*var obj = document.getElementById("LangEnglish");*/
// <% Session("Language") = "English"%>;
document.getElementById("Home_Language").value = 'English';
document.getElementById("frmHome").submit();
}
</script>
<div class="vmenu">
<form id="frmHome" action="Home" method="post"">
<a id="LangEnglish" href="/" onclick="SetLanguageEnglish();" >English</a>
<a id="LangSpanish" href="/" onclick="SetLanguageSpanish();"> Spanish</a>
<h3><%= Html.Label(Model.SubTitle) %></h3>
<ul class="sbe">
<li class="sbe"><%= Model.Menu1%></li>
<li class="sbe"><%= Model.Menu2%></li>
<li class="sbe"><%= Model.Menu3%></li>
<li class="sbe"><%= Model.Menu4%></li>
<li class="sbe"><%= Model.Menu5%></li>
</ul>
<input id="Home_PageName" name="PageName" type="hidden" value="" />
<input id="Home_Language" name="Language" type="hidden" value="" />
<input id="Home_PageTitle" name="PageTitle" type="hidden" value="" />
<input id="Home_SubTitle" name="SubTitle" type="hidden" value="" />
<input id="Home_Menu1" name="Menu1" type="hidden" value="" />
<input id="Home_Menu2" name="Menu2" type="hidden" value="" />
<input id="Home_Menu3" name="Menu3" type="hidden" value="" />
<input id="Home_Menu4" name="Menu4" type="hidden" value="" />
<input id="Home_Menu5" name="Menu5" type="hidden" value="" />
</form>
</div>
</asp:Content>
and my controller class code is as follow:
Public Class HomeController
Inherits System.Web.Mvc.Controller
' GET: /Home
<OutputCache(Duration:=1800, VaryByParam:="none")>
Function Index() As ActionResult
Try
Dim oHome As New Home.Home
Dim oHomeModel As New Home.HomeVM
If (HttpContext.Session("Language") Is Nothing) Then
HttpContext.Session("Language") = "English"
End If
oHomeModel.Language = HttpContext.Session("Language").ToString()
Return View("Index", GetCaption(oHomeModel))
Catch ex As Exception
Return Nothing
End Try
End Function
<HttpPost()>
<OutputCache(Duration:=1800)>
Function Index(ByVal oHomeModel As Home.HomeVM) As ActionResult
Try
If ((oHomeModel.Language IsNot Nothing) Or (oHomeModel.Language.ToString() <> "")) Then
HttpContext.Session("Language") = oHomeModel.Language.ToString()
End If
Return View("Index", GetCaption(oHomeModel))
Catch ex As Exception
Return Nothing
End Try
End Function
For some reason when i click on any link on my view page. The Controller class get is getting fired. due to this the value in session is not getting refreshed. Can anyone suggest why this is happening?
The following is my model class.
Namespace Home
Public Class HomeVM
'The following property are used as parameter
Public Property PageName As String
Public Property Language As String
'The following are the form caption peroperty
Public Property PageTitle As String
Public Property SubTitle As String
Public Property Menu1 As String
Public Property Menu2 As String
Public Property Menu3 As String
Public Property Menu4 As String
Public Property Menu5 As String
End Class
It looks to me like you are mixing up server side and client side code.
<% Session("Language") = "English"%>;
The above line you have in JavaScript gets executed when the page first loads which prints nothing to the page. If you look at the source you should see nothing other than the semi-colon ;.
When you click the link that calls SetLanguageSpanish() there is nothing to be executed in that function and the page is directed to '/'. Have a look at the page source.
Edit
Here is some updated code which will submit a hidden input with the id "language" and a value of the specified by the function parameter.
HTML
<form id="frmHome" method="post">
English
Spanish
<input type="hidden" id="language" name="language" value="" />
</form>
JavaScript
function SetLanguage(language) {
document.getElementById("language").value = language;
document.getElementById("frmHome").submit();
}
VB.NET I am not a VB guy so double check the syntax here
<HttpPost()>
<OutputCache(Duration:=1800)>
Function Index(ByVal language As string) As ActionResult
HttpContext.Session("Language") = language
'Do stuff
Return this.View()
End Function