2 textboxes in 2 form with the same name - asp.net

I have 2 similar form-blocks on form:
<form action="/2/All/Home/SubIdeaComment?SubIdeaID=5576&referrerUrl=http%3A%2F%2Flocalhost%3A1261%2F2%2FAll%2FHome%2FIdea%2F5575%3FreferrerUrl%3Dhttp%253A%252F%252Flocalhost%253A1261%252F2" method="post">
<textarea id="SubComment" name="SubComment" style="width: 80%"></textarea>
<br />
<input type="submit" class="InputBtn" value="Reply" />
<input type="reset" class="InputBtn" onclick="ShowHideReply($('#divSubIdeaReply5576'), $('#subIdeaButtons5576'))"
value="Cancel" />
<br />
<br />
</form>
and
<form action="/2/All/Home/SubIdeaComment?SubIdeaID=5577&referrerUrl=http%3A%2F%2Flocalhost%3A1261%2F2%2FAll%2FHome%2FIdea%2F5575%3FreferrerUrl%3Dhttp%253A%252F%252Flocalhost%253A1261%252F2" method="post">
<textarea id="SubComment" name="SubComment" style="width: 80%"></textarea>
<br />
<input type="submit" class="InputBtn" value="Reply" />
<input type="reset" class="InputBtn" onclick="ShowHideReply($('#divSubIdeaReply5577'), $('#subIdeaButtons5577'))"
value="Cancel" />
<br />
<br />
</form>
I need to call the same method of controller (MVC project):
[AcceptVerbs(HttpVerbs.Post)]
[ValidateInput(false)]
public ActionResult SubIdeaComment(int SubIdeaID, string SubComment, string referrerUrl)
{
if (User.Identity.IsAuthenticated && !String.IsNullOrWhiteSpace(SubComment))
_repository.AddComment(User.Identity.Name, SubIdeaID, null, SubComment);
return Redirect(referrerUrl);
}
but when first form submits - I have SubComment as empty. As I understand, reason is 2 fields have the same name. But there are in different forms.... How to do it correctly?

As you already correctly assumed, the id-attribute must be unique. I don't know exactly how those forms are being generated in your app, but maybe using templates can help you out.

Related

How to I pass multiple parameters from a form to a controller in Asp.net MVC core?

Hi I am trying to take the textboxes from a form and insert them as a parameter in a controller
Here is my Form
#{
ViewData["Title"] = "AddCar";
}
<h1>AddCar</h1>
<form asp-action="AddCar" method="post">
<div class="text-danger">#Validation.errorMessage</div>
<label>Make/Model: </label><br />
<input type="text" name="makeModel" value="Buick" /><br />
<label>Year: </label><br />
<input type="text" name="year" value="1998" /><br />
<label>Color: </label><br />
<input type="text" name="color" value="Red" /><br />
<label>Price: </label><br />
<input type="text" name="price" value="123123" /><br />
<label>Milage: </label><br />
<input type="text" name="milage" value="112312" /><br />
<button type="submit">Search</button>
</form>
and this is my controller
[HttpPost]
[Route("[area]/AddCar/{makeModel}/{milage}/{year}/{color}/{price}")]
public IActionResult AddCar(String makeModel, String milage, String year, String color, String price)
{
Car car = new Car(year, makeModel, price, milage, color);
return View();
}
Try this:
[HttpPost]
[Route("[area]/AddCar")]
public IActionResult AddCar([FromForm] Car car)
{
...
return View();
}
But the best practice is to use view model. Check this page
https://learn.microsoft.com/en-us/aspnet/core/mvc/views/working-with-forms?view=aspnetcore-3.1

How to hide form field while reusing same form

I am using the following form to save user information.
<%# taglib prefix="sf" uri="http://www.springframework.org/tags/form"%>
<sf:form action="someAction"method="post" commandName="backingBean">
<sf:input type="hidden" name="userId" path="userId" />
<sf:input type="text" placeholder="Username" name="username" path="username" />
<sf:input placeholder="Password" path="password" type="password" name="password" />
<sf:input placeholder="Email" name="email" type="email" path="email" />
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
<button type="submit" class="btn btn-default">Submit</button>
</sf:form>
When an user want to edit their information I am sending them the same form using hidden userId. But this time I wish not show password filed in the form. How do I do that.
You could try with an <c:choose><c:when.. that tests a condition and does what you want: "not show password filed in the form"

Paypal Form Tag issue in asp.net

Hello guys i have gatting issue on loading asp.net
i have using master page for sub pages there is a form tag in materpage i have add a paypal button in sub page which is this in master page form tag is also important
in masterpage <form id="form1" runat="server"></form>
and in sup age
<form id="form2" runat="server" name="Paypal" action="https://www.paypal.com/cgi-bin
/webscr"
method="post">
<input type="hidden" name="cmd" value="_cart" />
<input type="hidden" name="upload" value="1" />
<input type="hidden" name="business" value="
<%=System.Web.Configuration.WebConfigurationManager.AppSettings["email"] %>
" />
<input type="hidden" name="item_name_1" value="<%=Session["ItemName"].ToString()%>"
/>
<input type="hidden" name="amount_1" value="<%=Session["ItemCost"].ToString() %>"/>
<input type="hidden" name="quantity_1" value="1" />
<input type="hidden" name="currency_code" value="USD" />
<input type="hidden" name="return" value="<%=Session["returnUrl"].ToString() %>"/>
<input type="hidden" name="lc" value="Stronger" />
<input type="image" src="images/paynow.png" border="0" name="submit" alt="Make
payments with PayPal - it's fast, free and secure!"
style="background: url(images/update-account.png);"/>
</form>
I'm getting bug this with sub page below
Server Error in '/Project' Application.
A page can have only one server-side Form tag.
Description: An unhandled exception occurred during the execution of the current web
request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Web.HttpException: A page can have only one server-side Form tag.
anybody have a idea how can i use form tag in sub page also.
Please check how many <form> tags are written on your aspx/html page. Make sure you are not using masterpage for this page.
Hi I have found this solution and its work solution is below:
<form name="Paypal" action="https://www.paypal.com/cgi-bin/webscr"
method="post" target="_blank"></form>
<form name="Paypal" action="https://www.paypal.com/cgi-bin/webscr"
method="post">
<input type="hidden" name="cmd" value="_cart" />
<input type="hidden" name="upload" value="1" />
<input type="hidden" name="business" value="
<%=System.Web.Configuration.WebConfigurationManager.AppSettings["email"] %>
" />
<input type="hidden" name="item_name_1" value="<%=Session["ItemName"].ToString()%>"
/>
<input type="hidden" name="amount_1" value="<%=Session["ItemCost"].ToString() %>"/>
<input type="hidden" name="quantity_1" value="1" />
<input type="hidden" name="currency_code" value="USD" />
<input type="hidden" name="return" value="<%=Session["returnUrl"].ToString() %>"/>
<input type="hidden" name="lc" value="Stronger" />
<input type="image" src="images/paynow.png" border="0" name="submit" alt="Make
payments with PayPal - it's fast, free and secure!"
style="background: url(images/update-account.png);"/>
</form>
This code is working good as i want.
If you'are under a MasterPage Don't use the form tag as long as you're inside a form, just delete it, delete the hidden input that causes the SUBMIT and, instead, use the following ImageButton:
<asp:ImageButton ID="cmdpay" runat="server" PostBackUrl="https://www.paypal.com/cgi-bin/webscr" Text="" ImageUrl="images/paynow.png" ></asp:ImageButton>
It works

asp.net request.form

public ActionResult DisplayCustomer()
{
Customer objCustomer = new Customer();
objCustomer.Id = Convert.ToInt16(Request.Form["Customerid"]);
objCustomer.CustomerCode = Request.Form["code"];
objCustomer.Amount = Convert.ToDouble(Request.Form["amount"]);
return View(objCustomer);
}
This is my action in controller(MVC 2 aspx):
<form action="DisplayCustomer" method="post">
Customer id:-
<input type="text" id="Customerid" /><br />
Customer Code:-
<input type="text" id="code" /><br />
Customer amount:-
<input type="text" id="amount" /><br />
<input type="submit" value="click here" />
</form>
This is the view. When someone hit the submit button it is directed to a new page:
<div>
<b> ID</b> <%= Model.Id %><br />
<b> Code </b><%=Model.CustomerCode %><br />
<b> Amount</b> <%=Model.Amount %><br />
</div>
I always get 0 in Id and Amount while blank in CustomerCode.
Any clue? Why this is happening? What is wrong?
Your issue here is that you set the id, and not the name. Set the name to get the post back value. Eg:
<input type="text" name="Customerid" />
read also: HTML input - name vs. id in the line "Used on form elements to submit information"

two forms with same input id in asp.net mvc

I have multiple forms on a page which pass an id to the controller via hidden inputs. As I am using strongly typed views for these I think I need to keep the Id for each of these to be the same. It works currently though I think it's bad practice. How should I handle this? In Django there are form prefix values is there an equivalent?
Avoid duplication of form input element ID in Django
Here are the two forms I am using:
<form action="/Course/CropImage" method="post">
<input id="CourseId" name="CourseId" type="hidden" value="<%= Model.CourseId %>" />
<input id="X" name="X" type="hidden" value="<%= Model.X %>" />
<input id="Y" name="Y" type="hidden" value="<%= Model.Y %>" />
<input id="W" name="W" type="hidden" value="<%= Model.W %>" />
<input id="H" name="H" type="hidden" value="<%= Model.H %>" />
<input type="submit" value="Crop" />
</form>
<form action="/Course/UploadImage" enctype="multipart/form-data" method="post">
<input id="CourseId" name="CourseId" type="hidden" value="<%= Model.CourseId %>" />
<label for="Image">Select Image:</label><input id="Image" type="file" name="Select Image"/>
<input type="submit" value="Upload" />
</form>
If you are having 2 view models (one for the crop, one for the upload) you can prefix them like this (you can use html helpers):
<form action="/Course/CropImage" method="post">
<input id="Crop_CourseId" name="Crop.CourseId" type="hidden" value="<%= Model.CourseId %>" />
<input id="Crop_X" name="Crop.X" type="hidden" value="<%= Model.X %>" />
<input id="Crop_Y" name="Crop.Y" type="hidden" value="<%= Model.Y %>" />
<input id="Crop_W" name="Crop.W" type="hidden" value="<%= Model.W %>" />
<input id="Crop_H" name="Crop.H" type="hidden" value="<%= Model.H %>" />
<input type="submit" value="Crop" />
</form>
<form action="/Course/UploadImage" enctype="multipart/form-data" method="post">
<input id="Upload_CourseId" name="Upload.CourseId" type="hidden" value="<%= Model.CourseId %>" />
<label for="Image">Select Image:</label><input id="Upload_Image" type="file" name="Upload.Image"/>
<input type="submit" value="Upload" />
</form>
and then bind attribute with the prefix to you controller actions like this:
public ActionResult CropImage([Bind(Prefix="Crop")]CropViewModel viewModel)
{
// do something
}
public ActionResult UploadImage([Bind(Prefix="Upload")]UploadViewModel viewModel)
{
// do something
}
This is not a bad practise. They are completely different forms so that makes the input element unique. You will not make your server code or client js/markup any more semantic by adding prefixes.
I always prefix my column-names with the table name. Here's the database-layout of my latest MVC-project (using strongly typed views and LINQ to SQL):
WeblogEntries:
- WeblogEntryId
- WeblogEntryHeaderText
- WeblogEntryBodyText
- WeblogEntryDate
WeblogComments:
- WeblogCommentId
- WeblogCommentBodyText
- WeblogCommentDate
WeblogErrors
- WeblogErrorId
- WeblogErrorExceptionMessage
- WeblogErrorExceptionStackTrace
- WeblogErrorDate
These naming conventions work great with the entity classes that gets generated using dbml-files.

Resources