#Html.Beginform() not rendering properly IE7-8 - asp.net-mvc

I have a HTML form which is in a partial that is loaded via jquery.load(). My partial looks something like this:
#Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data", id = "addComicForm"}){
<div class="add-comic-submit">
<input type="submit" value="haha" name="haha" />
</div>
}
On IE7-8 It's not rendered properly and does not create a form attribute, however, if I manually insert the form code such as
<form action="/ManageComics/ComicAdder" enctype="multipart/form-data" id="addComicForm" method="post" novalidate="novalidate"> </form>
It works properly.

Because you are doing it wrong. It should be like this:
#using (Html.BeginForm("ComicAdder", "ManageComics", FormMethod.Post, new { enctype = "multipart/form-data", id = "addComicForm" }))
{
<div class="add-comic-submit">
<input type="submit" value="haha" name="haha" />
</div>
}

another way should like this:
#{
Html.BeginForm("your actionName", "your controllerName", FormMethod.Post);
}
<div class="add-comic-submit">
<input type="submit" value="haha" name="haha" />
</div>
#{
Html.EndForm();
}

Related

Multiple forms in one view page

I'm working on an application that handles employee's profile.
I have 3 forms (all in the same controller) in a single view page, but it only saves the 1st form. And when i save the 2nd form, it clears the values of the 1st and 3rd form.
Here is my code:
Views/EMPs/Index:
#using (Html.BeginForm("Edit", "EMPs", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div class="form-group">
<div class="pull-right">
<input type="submit" value="Update" name="personalsubmit" class="btn btn-success" />
</div>
</div>
}
#{ Html.RenderAction("Index", "EMP_REFERENCE", new { id = Model.eMP.lineno });}
#using (Html.BeginForm("Edit", "EMPs", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div class="form-group">
<div class="pull-right">
<input type="submit" value="Update" name="jobsubmit" class="btn btn-success" />
</div>
</div>
}
#{ Html.RenderAction("Index", "EMP_BENEFITS", new { id = Model.eMP.lineno });}
#using (Html.BeginForm("Edit", "EMPs", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div class="form-group">
<div class="pull-right">
<input type="submit" value="Update" name="otherssubmit" class="btn btn-success" />
</div>
</div>
}
EMPsController
public ActionResult Edit([Bind(Include = "lineno,EMPNO,IDNO..")] EMP eMP)
{
if (ModelState.IsValid)
{
if (Request.Form["personalsubmit"] != null)
{
db.Entry(eMP).State = EntityState.Modified;
db.SaveChanges();
}
if (Request.Form["jobsubmit"] != null)
{
db.Entry(eMP).State = EntityState.Modified;
db.SaveChanges();
}
if (Request.Form["otherssubmit"] != null)
{
db.Entry(eMP).State = EntityState.Modified;
db.SaveChanges();
}
return Redirect(Request.UrlReferrer.PathAndQuery);
}
return View(eMP);
}
I couldn't put them all in one form, because i used an ajax beginForm between them for another crud method. Since I've read that nested forms are not recommended.
Is there a way to save one form without it clearing the values of the other forms?
Is there a way to save one form without it clearing the values of the other forms?
You could simply use ajax.beginform for each of those forms;
How to use Simple Ajax Beginform in Asp.net MVC 4?
Or you could make your own ajax implementation
https://www.c-sharpcorner.com/blogs/using-ajax-in-asp-net-mvc
Or you could just go ahead and bind everything on your model;
// don't need to specify which properties to bind, all of the available properties in your view will be bound to the model on POST
public ActionResult Edit(EMP eMP)
{
if(eMP.FirstName!=null ...){
// ... do some checking depending on what values are submitted
}
// save profile here
// when you return the view
return View(eMP);
}
but for this method you need to only have 1 form for Personal,Job, and Others.
#{ Html.RenderAction("Index", "EMP_REFERENCE", new { id = Model.eMP.lineno });}
#{ Html.RenderAction("Index", "EMP_BENEFITS", new { id = Model.eMP.lineno });}
#using (Html.BeginForm("Edit", "EMPs", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<!--put all your employee input fields here-->
<div class="form-group">
<div class="pull-right">
<input type="submit" value="Update" name="submit" class="btn btn-success" />
</div>
</div>
<!--put all your job input fields here-->
<div class="form-group">
<div class="pull-right">
<input type="submit" value="Update" name="submit" class="btn btn-success" />
</div>
</div>
<!--put all your others input fields here-->
<div class="form-group">
<div class="pull-right">
<input type="submit" value="Update" name="submit" class="btn btn-success" />
</div>
</div>
}

MVC ASP.NET Map routing is not working with form GET request

In View-
#using (Html.BeginForm("PageName","ControllerName", FormMethod.Get))
{
<input type="hidden" name="categoryName" value="Insurance" />
<input type="hidden" id="cityName6" value="Irvine" name="cityName" />
<input type="hidden" name="page" value="1" />
<input type="submit" class="btn btn-default" value="Insurance" />
}
In RouteConfig-
routes.MapRoute(
"SomethingRouteName",
"{categoryName}/{cityName}/{page}",
new { controller = "ControllerName", action = "PageName" }
);
I want url to appear like this - Insurance/Irvine/1
But its coming like this- ControllerName/PageName?categoryName=Insurance&cityName=Irvine&page=1
This works fine when I use hyperlink instead of form get method.
#Html.ActionLink("Insurance", "PageName", "ControllerName", new{ categoryName = "Insurance", cityName = "Irvine", page = 1})
//URL shown: Insurance/Irvine/1 as expected.
But I have to use form GET method so this hyperlink way is useless.
Please help
You're not passing any route values to Html.BeginForm, so your rendered form element looks like this:
<form action="/ControllerName/PageName" method="get">
</form>
So when you click submit, it simply appends the values of the form as a query string.
To fix this:
#using (Html.BeginForm("PageName", "Home", new {categoryName = "Insurance", cityName = "Irvine", page = "1"}, FormMethod.Get))
{
<input type="submit" class="btn btn-default" value="Insurance" />
}

Form action not hitting MVC controller method

I want to do a simple file upload using Html forms. I have the following in my view:
<form action='#Url.Action("Save", "Order")' method="post" enctype="multipart/form-data" id="attachmentForm">
<div >
<label style="text-align: left;">Delivery note:</label>
</div>
<div style="float:left; ">
<input type="file" name="DeliveryNoteFile" id="DeliveryNote" style="width: 400px;" />
</div>
<div style="float:right; margin-top:10px; margin-left:5px; margin-bottom:0px;">
#(Html.Kendo().Button()
.Name("btnAddAttachment")
.HtmlAttributes( new {type = "submit"} )
.Content("Submit"))
</div>
</form>
Now here is my controller method. Controller name: Order , Method name: Save.
Why is it not hitting my controller method?
[HttpPost]
public ActionResult Save(HttpPostedFileBase file)
{
if (file != null)
{
var fileName = Path.GetFileName(file.FileName);
var physicalPath = Path.Combine(Server.MapPath("C:\\Attachments"), fileName);
file.SaveAs(physicalPath);
}
return Content("");
}
Note that this is only a first draft. Any suggestions to improve this are also welcome.
I think in your case your button is not of type submit that is why it is not hitting controller action just try making submit button this way:
#(Html.Kendo().Button()
.Name("btnAddAttachment")
.HtmlAttributes( new {type = "submit"} )
.Content("Submit"))
as # AbbasGaliyakot comment worked for the user in comment section so i m also including it here.
Change controller action parameter name from file to DeliveryNoteFile.
Please try this out. This would help.
#using (Html.BeginForm("Save", "Order", FormMethod.Post, new { enctype = "multipart/form-data", id = "attachmentForm" }))
{
<div >
<label style="text-align: left;">Delivery note:</label>
</div>
<div style="float:left; ">
<input type="file" name="DeliveryNoteFile" id="DeliveryNote" style="width: 400px;" />
</div>
<div style="float:right; margin-top:10px; margin-left:5px; margin-bottom:0px;">
#(Html.Kendo().Button()
.Name("btnAddAttachment")
.HtmlAttributes( new {type = "submit"} )
.Content("Submit"))
</div>
}
And in JS you need to bind the click function of your submit button like shown below:
$('#btnAddAttachment').bind('click', function () {
$('#attachmentForm').submit();
});
Thanks!

asp.net mvc #Html.BeginForm diplays unwanted text

I am developing a web application without any models. I just do not need them for what I am trying to do. I want to take some input from the view and to pass it on the controller. So far so good. The problem is that on the view I get some unwanted text: "System.Web.Mvc.Html.MvcForm"; it comes from the follwoing line: #Html.BeginForm("GetInfo", "Test", FormMethod.Post, new { id = "TestForm" } )
More precisely, my code is this:
--the view
#Html.BeginForm("GetInfo", "Test", FormMethod.Post, new { id = "TestForm" } )
<br/>
<legend>Report</legend>
<br />
<div class="editor-label">
<label>Date From</label>
</div>
<div class="editor-field">
<input name="dateFrom" type="date" required />
</div>
<br />
<input type="submit" class="btn btn-primary" />
and the method from the controller is :
public ActionResult GetInfo(DateTime dateFrom)
{
//some code
return RedirectToAction("Index");
}
Any ideas?
Thank you in advance! :)
Try
#using (Html.BeginForm(.....))
{
// Html here
}

The form element is not rendered when used inside Telerik Tabstrip

When I put a form inside a Telerik tabstrip, the form elements are not get rendered in the HTML. But input elements are rendered. This form is working fine outside tabstrip.
tabstrip.Add()
.Text("Variants")
.Content(() =>
{
%>
<form action="#" id="form1" method="post">
<input id="option" type="text" />
<input type="submit" value="save" /></span>
</form>
<%
});
try to use something like this :
tabstrip.Add()
.Text("Variants")
.Content(
{%><text>
%>
<form action="#" id="form1" method="post">
<input id="option" type="text" />
<input type="submit" value="save" />
</form>
<%
</text>
});
I have solved this problem
Kenod helper ignore first form if you put empty form and after that put your
actual form it will ignore the first form and take your form
#{Html.Kendo().TabStrip().Name("TabStrip").TabPosition(Model.TabStripPosition).Items(items =>
{
foreach (var tab in Model.TabItems)
{
items.Add()
.Text(tab.Title)
.Content(#
#Html.Raw("")
#using (Html.BeginForm("", "..","..", new { enctype = "multipart/form-data", id = "..." }))
{ #Html.Partial(tab.PartialView, tab.Model)}
</text>
).Selected(true)
}
}).Render();

Resources