I have a form
<form action="/" method="post">
<input type="file" name="myFile" />
<input type="submit" name="submit" value="submit" />
</form>
I also have some C# code
if (Request["submit"] == "submit") {
Response.Write(Request.Files.Count);
}
If a user chooses a file on their system and submits, What reasons could there be for me seeing a "0" instead of a "1" in the Request.Files.Count property?
Try adding the enctype attribute to your <form>
<form enctype="multipart/form-data" action="/" method="post">
If you are using any kind of ajax (i.e. update panel) then you have to make sure you're doing a full page post back when submitting not a partial page postback.
Related
<form action="#Url.Action("PokemonSteps", "PokemonView", new { id = Model.Id, steps = steps })">
<input type="number" name="steps" min="1" max="#Model.userSteps">
<input type="submit">
</form>
This form is a number box from 1 to the amount of steps the user has.
When they submit the number in the box, I want to pass it to #Url.Action as the steps in the steps = steps part.
How do I go about that (sorry for the really stupid question)
<form action="#Url.Action("PokemonSteps", "PokemonView")" method="post">
<input type="hidden" name="id" value="#Model.Id">
<input type="number" name="steps" min="1" max="#Model.userSteps">
<input type="submit" value="Submit">
</form>
Please add Attribute method="post" and share your controller method signature also...
Try doing something like this:
#using (Html.BeginForm("PokemonSteps", "PokemonView", FormMethod.Post)){
#Html.TextBoxFor(m=> m.userSteps)
<input type="submit" value="Submit" />
}
Hope this Helps!!
I am experiencing a very strange behavior. I am doing a simple ASP .NET application that will upload a file to the server.
I have a simple form that does this:
<form class="form-horizontal" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="files" class="col-md-3 col-sm-4 control-label">Bestand *</label>
<div class="col-md-9 col-sm-8" ">
<input name="file" type="file" id="file" required />
<span >#ViewBag.TheMessage</span>
</div>
</div>
<div class="form-group">
<div class="col-md-9 col-sm-8 col-md-push-3 col-sm-push-4">
<input type="submit" class="btn btn-primary" value="Versturen" />
</div>
</div>
</form>
The strange thing happens with the POST, 9 of 10 times this POST goes to test.com instead of localhost. I sniffed it with firebug to discover this. The firebug looks like this:
The Controller.cs looks like this, and it's working fine when post reached:
[HttpPost]
public ActionResult Index(HttpPostedFileBase file)
{
ViewBag.TheMessage = "Thanks, an e-mail will be sent shortly";
if (file.ContentLength > 0)
{
ViewBag.Message = "Your application description page.";
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
file.SaveAs(path);
}
return View();
}
Another strange thing is that if I just use an empty format cshtml file only with:
<form method="post" enctype="multipart/form-data">
<input type="file" name="file" id="file" />
<input type="submit"/>
POST woks fine every time.
Thanks.
Check to see if you have any Javascript that is changing your forms action attribute. I would start by looking at onload, onsubmit, or on your submit buttons onclick method. If you're using jquery be sure to check your $(document).ready () function. This really could be anywhere in your js though so make sure you look everywhere.
Another culprit could be that you have a base tag in your html that looks something like <base href="http:www.test.com /> which will change all of your relative urls to go that base location
i have a page with multiple forms like this: (all of them have different names
<form method="post" name="form1">
<input type="text>
<input type="text>
<input type="text>
<input type="text>
<button type="submit" name="button1" class="btn btn-icon btn-primary glyphicons circle_ok"><i></i>Save changes</button>
</form>
This is the code i'm using to check which form is submitted:
if(IsPost && !Request["button1"].IsEmpty()) {
}
the code above only works if i submit the form through an <input type="submit" name="button">
i wanted to know if there's any way to know which form is submitted with a button type=submit (the one that's in the form i posted above)
This is because the value of the a <button> element is not posted. You could add a hidden field to your form:
<input type="hidden" name="formname" value="myform" />
Then check for this in your code:
if(IsPost && Request["formname"] == "myform") {
}
This a html code in site that i want to use it in my web browser control C# :
<form name="frmWorkBookPrintTerm" action="WorkBookPrintTerm.asp" target="_blank" method="post">
<hr width="90%">
<input type="Hidden" name="strTermCode" value="912">
<input type="Hidden" name="strStudentCode" value="896325605">
<input type="Button" onclick="javascript:document.frmWorkBookPrintTerm.submit();" value="...">
<hr width="90%">
</form>
But It is not working. When i want to go ".../WorkBookPrintTerm.asp" directly website says you did illegal job and otherwise web browser control is not working.
Thank You.
I am developing a website using asp.net c# and I want to put a form inside the page. Now as aspx pages have the form tag I do not want to nest another form inside this as it will invalidate my html. But I need this form to use GET rather than POST. I know I can change the postback url in the asp:button. Can this be done without using logic in the codbehind?
Change the method to GET just for this form not every thing on the page
change the target to _blank if possible.
Example in html of what I want.
<form action="http://maps.google.co.uk/maps" method="get">
<p><label for="saddr">Your postcode</label>
<input type="text" name="saddr" id="saddr" value="" />
<input type="submit" value="Go" />
<input type="hidden" name="daddr" value="[destination]" />
<input type="hidden" name="hl" value="en" /></p>
</form>
you can use jquery to accomplish this
$(document).ready(function() {
$("#buttonId").click(function() {
$("#formId").attr("method", "get");
});
});
the above snippet, fires when the button with id 'buttonId' is clicked. it changes the method attribute of the form with id 'formId'
You can have multiple forms in an html. ASP.NET page also supports multiple form tags, however only one of then can be server side form (runat="server").
So I will suggest that you add another form tag within your page - some thing like
...
<body>
<form runat="server">
... server controls etc
</form>
<!-- your form -->
<form action="http://maps.google.co.uk/maps" method="get">
<p><label for="saddr">Your postcode</label>
<input type="text" name="saddr" id="saddr" value="" />
<input type="submit" value="Go" />
<input type="hidden" name="daddr" value="[destination]" />
<input type="hidden" name="hl" value="en" /></p>
</form>
</body>
</html>
Note that you cannot put any server side control in your html tag. So you have to use html controls and manage them within page code using Request object.