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"
Related
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
i have created this file in visual studio 2012 . The first page of survey works completely fine but when it re directs to display its showing '500 internal server error. The project has no compilation error . i am opening this file using filezilla can anyone please help me . its a simple program which fetches the value from survey.asp and displays the value in the next form in a tabular format.
------ survey.asp-----------------------------
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>Survey Form</title>
</head>
<body>
<form method="post" action="display.asp">
<br/>
Please Enter Your Age <input type="text" name="first" value="<%=request("Age")%>"/> <br /> <br/>
Please Enter Your Salary $ <input type="text" name="sal" value="<%=request("salary")%>"/> <br /><br/>
What level of Education Have You Completed : <br/>
<input type="radio" name="radiobutton" value="<%=request("education")%>"/> Some High School <br />
<input type="radio" name="radiobutton" value="<%=request("education")%>"/> Completed high School <br />
<input type="radio" name="radiobutton" value="<%=request("education")%>"/>Some College Education <br />
<input type="radio" name="radiobutton" value="<%=request("education")%>"/> Completed a B.S Degree <br />
<input type="radio" name="radiobutton" value="<%=request("education")%>"/> Completed a Master Degree <br /> <br/>
<input type="submit" value="Send Survey" />
</form>
</body>
</html>
----------display.asp---------------------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%
Age = request.form("first")
salary = request.form("sal")
education = request.form("radiobutton").ToString();
if age="" or salary="" or education="" then
%>
<html>
<head><title></title></head>
<body>
You must enter all the info
<form method="post " action="survey.asp">
<input type="hidden" name="age" value="<%=age%>" />
<input type="hidden" name="salary" value="<%=salary%>" />
<input type="hidden" name="education" value="<%=education%>" /> <br/>
<input type="submit" value="Return" />
</form>
</body>
</html>
<%
response.end
end if
%>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Display Name</title>
</head>
<body>
You entered: <br />
<table>
<tr>
<td> <%=age%> </td>
<td> <%=salary%> </td>
<td> <%=education %> </td>
</tr>
</table>
<br />
</body>
</html>
This is classic asp with vbscript, not asp.net with C#. You don't get compilation errors with classic because it isn't compiled, it's interpreted at runtime.
You appear to have put a line of C# in display.asp - if you replace the line
education = request.form("radiobutton").ToString();
with
education = Cstr(request.form("radiobutton"))
This should solve your initial 500 error. Enclosing within Cstr() isn't actually necessary, but it's the VBScript way of converting to a string.
I don't think your radio button group will work as you have coded it. If you're trying to preserve the original submitted value you should do something like this.
<input type="radio" name="radiobutton" value="1" <% If request.form("education") = "1" Then Response.write "checked" End if %> /> Some High School <br />
<input type="radio" name="radiobutton" value="2" <% If request.form("education") = "2" Then Response.write "checked" End if %> /> Completed high School <br />
<input type="radio" name="radiobutton" value="3" <% If request.form("education") = "3" Then Response.write "checked" End if %> />Some College Education <br />
<input type="radio" name="radiobutton" value="4" <% If request.form("education") = "4" Then Response.write "checked" End if %>/> Completed a B.S Degree <br />
<input type="radio" name="radiobutton" value="5" <% If request.form("education") = "5" Then Response.write "checked" End if %>/> Completed a Master Degree <br /> <br/>
I'd suggest that having a second form on a different page isn't the best way to do form validation, better to have the form post to itself and use conditional statements to make error messages appear beside the individual form elements if nothing if they're left empty.
Finally, look at this page - it tells you how to enable useful error messages in Classic ASP.
http://www.chestysoft.com/asp-error-messages.asp
My click event is showing the error "the name login_password and login_username does not exist in the current context" when I am trying to access my text box from their ids. please help me identify the error as I am just a beginner.
click event:
protected void LogIn_Clicked(object sender, EventArgs e)
{
string usern = "abc";
string pass = "123";
if (login_username.Text == usern && login_password.Text == pass)
{
Response.Redirect("Default.aspx");
}
else
{
label1.Text = "LogIn";
}
}
}
login form:
<form id="login" runat="server">
<h1 style = "Background-color: deepskyblue;">
Log in to your account!</h1>
<p class="register">
<a style = "color: deepskyblue;" href="Login.aspx?logoff=true">Logoff</a>
</p>
<div>
<label for="login_username">
Username</label>
<input type="text" name="username" id="login_username" class="field required" title="Please provide your username" onclick="return login_username_onclick()" />
</div>
<div>
<label for="login_password">
Password</label>
<input type="password" name="password" id="login_password" class="field required"
title="Password is required" />
</div>
<div class="submit">
<asp:Button ID="Button1" Text="Log in" runat="server" OnClick="LogIn_Clicked"
BackColor="#00B2EE" ForeColor="White" />
Those aren't Asp.net Controls. Add the runat="server" attribute:
<input type="text" name="username" id="login_username" class="field required" title="Please provide your username" onclick="return login_username_onclick()" runat="server"/>
Or you can add Asp.net controls:
<asp:TextBox runat="server" ID="login_username" CssClass="field required" ToolTip="Please provide your username"></asp:TextBox>
your inputs for username and password are html only - they need a runat="server" tag to get them in the code behind. The usual way is to use an asp:Textbox though.
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.
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.