Hi i have this project where i have an string parameter on a page the form make the post to
/booking/confirm
but it need to post to
/Booking/Confirm?guid=013b0053-5840-4866-97c2-d544d8b6a34c
i could ofcourse just write
#using (Html.BeginForm())
but i need an class on the form for an jquery function
so my using looks like this
#using (Html.BeginForm("Confirm", "Booking", FormMethod.Post, new { #class = "sigPad"}))
Write html without using the helper
<form action="#Url.Action("Prods","Products", new { guid=013b0053-5840-4866-97c2-d544d8b6a34c })" method="POST" class="sigPad">
...
</form>
#using (Html.BeginForm("Confirm", "Booking", new { guid=013b0053-5840-4866-97c2-d544d8b6a34c }
Related
I tried to use 2 partial views under 1 view/page - first half page for search filters & 2nd half page for table display. Its working also,but the issue is the white color content panel is in fixed size. i.e., if my table got data it comes out of the white color panel.
<section class="content admin_table">
#RenderBody()
</section>
I use above class in Layout. In Index View, I render both Partial views. My code part is below
#Html.Partial("_view1", Model.Filter)
#Html.Partial("_view2", Model.Result)
My IndexPage
#model SW.Web.ViewModels.CommonVM
#{
Layout = "~/Views/Shared/_Layout1.cshtml";
ViewBag.Title = "Title";
ViewBag.Header = "Details";
}
<link href="~/Content2/plugins/datatables/dataTables.bootstrap.css" rel="stylesheet" />
<!DOCTYPE html>
#section JavaScript{
<Script> .....</script>
<Script> .....</script>
}
#Html.Partial("_view1", Model.Filter)
#Html.Partial("_view2", Model.Result)
and My Partial view 1
#model SW.Web.ViewModels.viewmodel1
#{
using (Html.BeginForm("Index", "Details", FormMethod.Post, new { d= Model }))
{
#Html.ValidationSummary(true)
<div>.....Design</div>
}
and my partial view 2
#model IEnumerable<SW.Web.ViewModels.Viewmodel2>
#{
<div> Designs for partial view 2</div>
}
I understand this is over a year old but just in case someone stumbles across this post.
RenderPartialView is written like the following:
#{#Html.RenderPartial("_yourPartial", yourModel)}
So create One view with
Search partial view like
//Serach view
<div class="white">
<h2>Search view</h2>
<--! search coading-->
#Html.RenderPartial("Table_View", objectvalue2);
</div>
Use below line
#Html.RenderPartial("_SearchView", objectvalue1);
in another page
by controller action
#Html.Action("action1", "Controller", new { id = Model.idOfPart1 })
#Html.Action("action2", "Controller", new { id = Model.idOfPart2 })
by rendering view
# Html.Render ("ParialView1", Model.model1)
# Html.Render ("PartialView2", Model.model2)
In my solution "WebApp1" has two projects "WebApp1.Web" and "WebApp1.Api", in each project has two controllers "AccountsController" and "ApplicationsController" (same name)
In WebApp1.Web there are views:
In the create application view it's calling the correct controller WebApp1.Web.ApplicationsController
<form asp-controller="Applications" asp-action="Create" method="post">
Translates to
<form method="post" action="/Applications/Create" novalidate="novalidate" _lpchecked="1">
This is the correct behavour
But ... in the create account view it is calling the wrong controller WebApp1.Api.AccountsController
<form asp-controller="Accounts" asp-action="Create" method="post">
Translates to
<form method="post" action="/api/Accounts" novalidate="novalidate" _lpchecked="1">
Its not suppose to post to /api/Accounts, its suppose to post to /Accounts/Create
Question
Why is the view calling the wrong controller? its not calling the web controller inside the same webapp1.web? How do I specific the view to use the controller webapp1.web?
You can specify where to post a form in .NET MVC like this:
#using (Html.BeginForm("Create", "Accounts", new object { }, FormMethod.Post, new { enctype = "multipart/form-data" })) {
<!-- Your content goes here -->
}
I am trying to upload a .csv file into an SQL database, but the controller action does not seem to be grabbing the file at all. Check out the code, I must be missing a piece:
View:
#using (Html.BeginForm("UploadValidationTable", "Home", FormMethod.Post))
{
<table style="margin-top: 150px;">
<tr>
<td>
<label for="csvFile">Filename:</label>
</td>
<td>
<input type="file" name="csvFile" id="csvFile"/>
</td>
<td><input type="submit" value="Upload"/></td>
</tr>
</table>
}
Controller:
[HttpPost]
public JsonResult UploadValidationTable(HttpPostedFileBase csvFile)
{
var inputFileDescription = new CsvFileDescription
{
SeparatorChar = ',',
FirstLineHasColumnNames = true
};
var cc = new CsvContext();
var filePath = uploadFile(csvFile.InputStream);
var model = cc.Read<OutstandingCreditCsv>(filePath, inputFileDescription);
try
{
var entity = new OutstandingCreditCsv();
foreach (var item in model)
{
entity.PoNumber = item.PoNumber;
entity.CreditInvoiceDate = item.CreditInvoiceDate;
entity.CreditInvoiceNumber = item.CreditInvoiceNumber;
entity.CreditInvoiceAmount = item.CreditInvoiceAmount;
}
}
catch(LINQtoCSVException ex)
{
}
return Json(model, "text/json");
}
csvFile is just appearing as null, no clue what could be going on since it is named in the view and I have the post method surrounding it. It makes it down until var filePath = uploadFile(csvFile.InputStream); and then breaks since the method is trying to pass a null value. Thanks!
I came across this when trying to upload files into db using MVC a while back, I was using scaffolded views (so it also passed back an object with the form), so it might be a bit different.
In your HTML.BeginForm() call you'll need to add the attribute enctype="multipart/form-data"
#using (Html.BeginForm("UploadValidationTable", "Home", FormMethod.Post, new {enctype="multipart/form-data"}))
I think you need to add enctype='multipart/form-data' to your <form>
using (Html.BeginForm("UploadValidationTable", "Home", FormMethod.Post,
new { enctype = "multipart/form-data" }))
You need to change your form declaration. Try adding enctype = "multipart/form-data" to your form declaration.
#using (Html.BeginForm("UploadValidationTable", "Home", FormMethod.Post, new { enctype = "multipart/form-data"}))
You need to add the following to the form declaration:
#using (Html.BeginForm("UploadValidationTable", "Home", null,FormMethod.Post, new { enctype = "multipart/form-data" }))
I am creating a page with a search property. The user will enter a number in the search field and hit the submit button. I need to be able to call a controller method in order to run the search code.
For now I am just trying to hit a partial page to just get some functionality in there. Below is the code I have so far. As of now nothing happens when I click the button. I hear Ajax was something to use so I have been playing with that a little. I am still learning the framework so bear with me.
<div class="span6 roundedCorners">
<div class="row-fluid Title">
<div class="span6">
Search Area
</div>
</div>
<div class="row-fluid individual">
<div class="row-fluid">
<div class="span4"><span class="pull-right fieldDescription">Number</span></div>
<div class="span8"><input /></div>
</div>
<div class="row">
<div class="span12" id="adminSubmitButton">
#using (Ajax.BeginForm(new AjaxOptions { UpdateTargetId = "_adminDetails" }))
{
<button type="submit" class="btn btn-inverse">Submit</button>
}
</div>
</div>
</div>
Your form is empty, it only has a submit button. You will need to move your search button inside the form. Something like this:
Model
public class SearchModel
{
[Required]
public string Query { get; set; }
}
View
#model SearchModel
...
#using (Ajax.BeginForm(new AjaxOptions { UpdateTargetId = "_adminDetails" }))
{
#Html.EditorFor(m => m.Query)
#Html.ValidationMessageFor(m => m.Query)
<button type="submit" class="btn btn-inverse">Submit</button>
}
<div id="_adminDetails"></di>
Note: Make sure you have an element with id _adminDetails
Your controller will have to take the model and perform the search. Example:
Controller
[HttpPost]
public ActionResult Index(SearchModel model)
{
//do something with your model (perform search)
return View(results);
}
Alright, I have it partially working now. When i click the button I am able to call the controller method using:
function adminButton()
{
$("#adminDetails").load("#Url.Action("ControllerMethod", "ControllerName")");
}
The #Url.Action helper allows me to call the method which returns a partial view. This is a step in the right direction. But from what I am reading I should be able to use the following Url helper instead of the #Url.Action:
"#Url("ControllerMethod", "ControllerName")/" + submitButton.Value
I have tried some variations and I am still doing some reading to figure out a solution. Is there a helper that would let me do this?
Thanks for the help guys. I did end up getting my problem solved friday evening. Basically what I did was load the partial page when the button was clicked through the help of JS/jQuery and the Razor #Url.Action() to call the controller method that returns a PartialView:
<script type="text/javascript">
function adminButton()
{
var link = "#Url.Action("ControllerMethod", "ControllerClass", new {
number = -1})";
var num = parseInt($("#number").val());
link = link.replace(-1, num);
$("#details").load(link);
}
</script>
Problem is : I do have Index.cshtml as
#{
ViewBag.Title = "Search";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<script src="#Url.Content("../../Scripts/jquery.unobtrusive-ajax.min.js") type="text/javascript"></script>
<h2>Answer's are </h2>
<div id="result">
#Ajax.ActionLink("Search Wiki",
"WikiAns",
new AjaxOptions{
UpdateTargetId = "result",
InsertionMode=InsertionMode.Replace,
HttpMethod="GET",
LoadingElementId="progress"
})
</div>
<div id="progress" style="display: none">
<img src="#Url.Content("../../Content/Images/ajax-loader.gif")" alt="loader" />
</div>
Now i do have partial view model _WikiAns:
#model IEnumerable<Questions.Models.WikiAnsClass>
<h2>Our Links</h2>
<table>
#foreach (var item in Model) {
<tr>
<td>
#item.content
</td>
</tr>
}
</table>
Now using above i want is that...on clicking Search Wiki action link, id="result" get all partial view rendered in it. But its not happening. Instead of its going to a page "localhost:80/Search/WikiAns" and showing the result there. but i want to it to stay at localhost:80/Search and replace its "result" id. but its not working this way.
Here is my action called WikiAns
public ActionResult WikiAns()
{
//Code to follow...
var wikians = //code to follow
return PartialView("_wikiAns", wikians);
}
What's the problem ?
Also how can i implement this Ajax GIF loader...i mean showing it while action gets executed. ?
Please suggest
Thanks
#Pbirkoff may be right about this but you can go through my post of very easy to implement demo of your scenario and build it up from there.
http://mazharkaunain.blogspot.com/2011/05/aspnet-mvc-razor-render-partial-view.html
http://mazharkaunain.blogspot.com/2011/04/aspnet-mvc-render-partial-view-using.html
You need to include Microsoft Ajax scripts at your page or layout.