How to change the form controls id and name in Razor Pages - asp.net

I have a contact Razor page backed by a ViewModel called ContactViewModel. The html generated is below.
<form method="post">
<div class="form-group">
<input placeholder="First Name" class="form-control" type="text" id="ContactViewModel_FirstName" name="ContactViewModel.FirstName" value="" />
</div>
<div class="form-group">
<input placeholder="Last Name" class="form-control" type="text" id="ContactViewModel_LastName" name="ContactViewModel.LastName" value="" />
</div>
<input type="submit" value="SUBMIT" class="btn btn-default" />
</form>
I have another Razor Page with a form backed by ShippingAddressViewModel.
<form method="post">
<div class="form-group">
<input placeholder="First Name" class="form-control" type="text" id="ShippingAddressViewModel_FirstName" name="ShippingAddressViewModel.FirstName" value="" />
</div>
<div class="form-group">
<input placeholder="Last Name" class="form-control" type="text" id="ShippingAddressViewModel_LastName" name="ShippingAddressViewModel.LastName" value="" />
</div>
<div class="form-group">
<input placeholder="Zip Code" class="form-control" type="text" id="ShippingAddressViewModel_ZipCode" name="ShippingAddressViewModel.ZipCode" value="" />
</div>
<input type="submit" value="SUBMIT" class="btn btn-default" />
</form>
I want to apply JS client validation to the FirstName and LastName. Since the name and id of the controls are different, I have to create two JavaScript methods to target the controls.
Is there a way in Razor Pages to back the page with different view models but the id and name are the same?

This problem is caused because you have an object. You can create two different variables in your PageModel.
[BindProperty]
public string FirstName { get; set; }
[BindProperty]
public string LastName { get; set; }
Now you your view can have the following code:
<input asp-for="FirstName" />
<input asp-for="LastName" />
This will have as a result to have the following html:
<input type="text" id="FirstName" name="FirstName" value="" />
<input type="text" id="LastName" name="LastName" value="" />
I hope it helps.

Related

The model item passed into the ViewDataDictionary is of type but this ViewDataDictionary instance requires a model item of type 'System.Collections

When I Want to post my Form in Asp.net.Mvc I get this Problem How can i fix that
InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'FormValidation.Models.Entity.Personel', but this ViewDataDictionary instance requires a model item of type 'System.Collections.Generic.IEnumerable`1[FormValidation.Models.Entity.Personel]'.
Controller
[HttpGet]
public IActionResult Index()
{
return View();
}
[HttpPost]
public IActionResult Post(Personel personel)
{
return View(personel);
}
form index
#model FormValidation.Models.Entity.Personel
<form method="post" asp-action="Post" asp-controller="Home">
<div class="container">
<div class="row">
<div class="form-group">
<label for="Name" class="form-control" placeholder="Enter Your Name">Name</label>
<input type="text" class="form-control" asp-for="Name" />
</div>
<br />
<div class="form-group">
<label for="Surname" class="form-control" placeholder="Enter Your Surname">Surname</label>
<input type="text" class="form-control" placeholder="Enter Your surname" asp-for="Surname" />
</div>
<br />
<br />
<div class="form-group">
<label for="City">City select</label>
<select class="form-control" asp-for="City">
<option></option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
</div>
<br />
<br />
<br />
<div class="form-group">
<label for="Age" class="form-control" placeholder="Enter Your Age">Age</label>
<input type="number" class="form-control" placeholder="Enter Your Age" asp-for="Age" />
</div>
<br />
<div class="form-group">
<label for="Adress">Adres</label>
<textarea class="form-control" rows="3" asp-for="Adress">Adres</textarea>
</div>
</div>
</div>
<br />
<input type="submit" name="submit" value="submit" class="btn btn-primary" />
</form>
Post index
#model IEnumerable<FormValidation.Models.Entity.Personel>
#foreach (var item in Model)
{
<p>#item.Name</p>
<p>#item.Surname</p>
<p>#item.Age</p>
<p>#item.City</p>
}
just as the error indicates,you passed the single personel model to render the view with return View(personel);
However,the View requires IEnumerable<Personel>
If you just want to show the details of the model you just created ,modify your view :
#model FormValidation.Models.Entity.Personel
.......
<p>#Model.Name</p>
<p>#Model.Surname</p>
<p>#Model.Age</p>
<p>#Model.City</p>

how to send Id using httppost Securely

look at follwing Sample Codes
I easily can change id and then edit another movie
how can I have a secure form?
<form action="/movies/Edit/4" method="post">
<input name="__RequestVerificationToken" type="hidden" value="UxY6bkQyJCXO3Kn5AXg-6TXxOj6yVBi9tghHaQ5Lq_qwKvcojNXEEfcbn-FGh_0vuw4tS_BRk7QQQHlJp8AP4_X4orVNoQnp2cd8kXhykS01" /> <fieldset class="form-horizontal">
<legend>Movie</legend>
<input data-val="true" data-val-number="The field ID must be a number." data-val-required="The ID field is required." id="ID" name="ID" type="hidden" value="4" />
<div class="control-group">
<label class="control-label" for="Title">Title</label>
<div class="controls">
<input class="text-box single-line" id="Title" name="Title" type="text" value="GhostBusters" />
<span class="field-validation-valid help-inline" data-valmsg-for="Title" data-valmsg-replace="true"></span>
</div>
</div>
<div class="control-group">
<label class="control-label" for="ReleaseDate">Release Date</label>
<div class="controls">
<input class="text-box single-line" data-val="true" data-val-date="The field Release Date must be a date." data-val-required="The Release Date field is required." id="ReleaseDate" name="ReleaseDate" type="date" value="1/1/1984" />
<span class="field-validation-valid help-inline" data-valmsg-for="ReleaseDate" data-valmsg-replace="true"></span>
</div>
</div>
<div class="control-group">
<label class="control-label" for="Genre">Genre</label>
<div class="controls">
<input class="text-box single-line" id="Genre" name="Genre" type="text" value="Comedy" />
<span class="field-validation-valid help-inline" data-valmsg-for="Genre" data-valmsg-replace="true"></span>
</div>
</div>
<div class="control-group">
<label class="control-label" for="Price">Price</label>
<div class="controls">
<input class="text-box single-line" data-val="true" data-val-number="The field Price must be a number." data-val-required="The Price field is required." id="Price" name="Price" type="text" value="7.99" />
<span class="field-validation-valid help-inline" data-valmsg-for="Price" data-valmsg-replace="true"></span>
</div>
</div>
<div class="form-actions no-color">
<input type="submit" value="Save" class="btn" />
</div>
</fieldset>
</form>
You can include the request path into the AntiForgery Token.
Declare a class that implements IAntiforgeryAdditionalDataProvider like this
public class AntiForgeryAdditionalDataProvider : IAntiforgeryAdditionalDataProvider
{
public string GetAdditionalData( HttpContext context )
{
return context.Request.Path.Value;
}
public bool ValidateAdditionalData( HttpContext context, string additionalData )
{
return context.Request.Path.Value == additionalData;
}
}
and register that class Startup
public class Startup
{
...
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices( IServiceCollection services )
{
// add this at first!
services.TryAddSingleton<IAntiforgeryAdditionalDataProvider, AntiForgeryAdditionalDataProvider>();
services.AddControllersWithViews();
}
...
}
Now every AntiForgeryToken is build and validated with the path /movies/Edit/4

Generated form asp-action tag helper includes id

I have the following form
<form asp-action="Edit">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<input type="hidden" asp-for="Id" />
<div class="form-group">
<label asp-for="Title" class="control-label"></label>
<input asp-for="Title" class="form-control" />
<span asp-validation-for="Title" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="ReleaseDate" class="control-label"></label>
<input asp-for="ReleaseDate" class="form-control" />
<span asp-validation-for="ReleaseDate" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Genre" class="control-label"></label>
<input asp-for="Genre" class="form-control" />
<span asp-validation-for="Genre" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Price" class="control-label"></label>
<input asp-for="Price" class="form-control" />
<span asp-validation-for="Price" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Save" class="btn btn-primary" />
</div>
</form>
After running the project and going to Edit action view, i see in the source code in browser that <form asp-action="Edit">is translated into <form action="/Movies/Edit/2" method="post">
My question is, how does ASP know about the id (2) in "/Movies/Edit/2".
In this markup the ASP defines what is its ID to be passed in POST when its form is sent to the controller.
<input type="hidden" asp-for="Id" />
A hidden field allows your entity Id to be kept in the form without being seen or modified by users when a form is submitted.
See this documentation below as a supplement to your question.
https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/controller-methods-views?view=aspnetcore-2.2
Thanks!
The edit form should always be delivered from an URL that has an ID in the URL according to our routing rules, something like /Movies/edit/1.
The form is always going to post back to that same URL, /Movies/edit/1.
The MVC framework will be able to pull that ID out of the URL and pass it as a parameter.
Refer to https://www.tutorialspoint.com/asp.net_core/asp.net_core_razor_edit_form.htm

Set a Virtual Page View with Universal GA

This is my HTML from. and i'm trying to set a Virtual Page View when submitting the form but i just can't make it work.
Pls advice
<form id="frmContact" action="post.php" method="post">
<div class="form-right-pnl">
<input type="text" class="required name" size="40" placeholder="×©× ×ž×œ×:" name="full_name">
<input type="text" class="required phone" size="40" placeholder="טלפון:" name="phone">
<input type="text" class="required email" size="40" placeholder='דו×"ל:' name="email">
<span>מ×שר קבלת ×—×•×ž×¨×™× ×¤×¨×¡×•×ž×™×™×</span>
<input type="checkbox" class="radio_btn" name="selling_a_property" checked value="Yes"/>
</div>
<div class="form-left-pnl" id='basic-modal'>
<input class="ddl_list" type="submit" value="" onClick=”_gaq.push([‘_trackEvent’, ‘Forms’, ‘Submit’]);” />
</div>
<div class="clear"></div>
</form>
Change this line
<input class="ddl_list" type="submit" value="" onClick=”_gaq.push([‘_trackEvent’, ‘Forms’, ‘Submit’]);” />
for
<input class="ddl_list" type="submit" value="" onClick=”_gaq.push(['_trackPageview', '/virtual-page-view-on-click-submit-button']);” />

How to post values to a page which is not on the same domain

I have this html code on a webpage:
<form action="" type=post>
<label for="name"><div><br>Account</br></div></label> <input type=text id="name" name="account" size=20 maxlength=32 /><br>
<label for="name"><div><br>Password</br></div></label> <input type=password id="name" name="password" size=20 maxlength=14 /><br>
<button type=submit>Okay</button>
</form>
How can I send from my asp.net webproject values on the other page in the "Account" and "Password" labels?
The page which contains the labels is not on the same domain.
You can put an absolute URL in the action attribute, like so:
<form action="https://othersite.com/login.aspx" type=post>
<label for="name"><div><br>Account</br></div></label> <input type=text id="name" name="account" size=20 maxlength=32 /><br>
<label for="name"><div><br>Password</br></div></label> <input type=password id="name" name="password" size=20 maxlength=14 /><br>
<button type=submit>Okay</button>
</form>
Does this not work for you?

Resources