Error Post Method with Partial View - c#

I have the following error when trying to post a partial view form on my page:
An exception of type 'System.Web.HttpException' occurred in System.Web.dll but was not handled in user code
Additional information: Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'.
The error is related to the line below within the main view
The Main View is:
#{
ViewBag.Title = "Home Page";
}
<div class="jumbotron">
#Html.ActionLink("Create a Club", "SelectClubType", "Club")
</div>
<div>
#Html.Action("SelectClubType", "Club")
</div>
<div class="row">
<p>something here</p>
</div>
The Partial View is:
#model GRCWebApp.ViewModels.SelectClubTypeViewModel
#{
Layout = null;
}
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-group">
<div class="row col-md-12">
<h3>Create a new Club - Select the type</h3>
</div>
<div class="row col-md-12">
<div class="col-md-3">
#Html.DropDownListFor(model => model.SelectedClubTypeId, new SelectList(Model.ClubTypes, "ClubTypeId", "ClubTypeName"), new { #class = "form-control" })
</div>
<div class="col-md-2 ">
<input type="submit" value="Create" class="btn btn-success" />
</div>
</div>
</div>
}
The Post Method:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult SelectClubType(SelectClubTypeViewModel model)
{
int id = model.SelectedClubTypeId;
return RedirectToAction("Create", new { id = id });
}

Related

Pager in .Net Core 2.1

I have added ReflectionIT.Mvc.Paging from NuGet Link but I have a problem.
In a controller i have 2 methods, Index and Organizations. When I am on the view of Orginizations and press page with number "2" in controller goes to index and not on Organizations method.
How to force it to go on a method I want or to extend this #await this.Component.InvokeAsync("Pager", new { pagingList = this.Model }) to pass method name as parameter?
Controller:
public IActionResult Index()
{
return View();
}
public async Task<IActionResult> Organizations(int page=1)
{
var userlist = _context.Users.Include(u => u.UserRoles).ThenInclude(u => u.Role).Where(o => o.UserRoles.All(r => r.Role.Name == "Company") && o.IsActive == true).AsNoTracking().OrderByDescending(o => o.Company);
var model = await PagingList.CreateAsync(userlist, 2, page);
return View(model);
}
View:
#model ReflectionIT.Mvc.Paging.PagingList<CharityProject.Models.ApplicationUser>
#using ReflectionIT.Mvc.Paging
#addTagHelper *, ReflectionIT.Mvc.Paging
#{
ViewData["Title"] = "Organizations";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="container py-lg-5 py-md-5 py-sm-4 py-4">
<h2 class="pageTitles">Organizations</h2>
<div class="row">
<nav aria-label="NewsFeed navigation example">
#await this.Component.InvokeAsync("Pager", new { pagingList = this.Model })
</nav>
<br />
#foreach (var item in Model)
{
<div class="col-lg-4 col-md-6 col-sm-6 product-men women_two">
<div class="product-toys-info">
<div class="men-pro-item">
<div class="men-thumb-item">
#if (item.Logo != null)
{
<img src=#Url.Content(item.Logo.Replace("//","/").Replace("///","/")) class="img-thumbnail img-fluid" alt="">
}
<div class="men-cart-pro">
<div class="inner-men-cart-pro">
View
</div>
</div>
</div>
<div class="item-info-product">
<div class="info-product-price">
<div class="grid_meta">
<div class="product_price">
<h4>
<a href=#Url.Action("OrganizationInfo","Home",new { id=item.Id})>#item.Company</a>
</h4>
<p>#item.Moto</p>
</div>
</div>
</div>
<div class="clearfix"></div>
</div>
</div>
</div>
</div>
}
<br />
<nav aria-label="NewsFeeds navigation example">
<vc:pager paging-list="#Model" />
</nav>
</div>
</div>
You can set the Action property to PagingList object :
var model = await PagingList.CreateAsync(userlist, 2, page);
model.Action = "Organizations";

how to set dynamic controller name in the partial page

I have a partial page it is using two View page different controller but there have same Edit Action method. i need to set controller name dynamically for particular page in the run time . how can i do it ?
My partial page _SearchProduct in the shared folder it is working only for WomanController action method Edit:
<div class="row">
<div class="col-md-6">
#using (Html.BeginForm("Edit", "Woman", FormMethod.Get))
{
<p>
Find by Product Id : #Html.TextBox("SearchString", ViewBag.CurrentFilter as string)
<input type="submit" value="Search" />
</p>
}
</div>
<div class="col-md-6">
<span style="color:green">#ViewBag.ProductName </span>
</div>
<span style="color:red"> #ViewBag.FindResult </span>
</div>
My WomanController EDIT page :
#model MKL.Models.WomanProduct
<hr />
#Html.Partial("~/Views/Shared/_SearchProduct.cshtml")
<hr />
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
#Html.ValidationSummary(true)
#Html.HiddenFor(model => model.WomanProductId)
#if (ViewBag.ProductId != null)
{
#Html.Hidden("ProductId", (int)ViewBag.ProductId)
}
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
My ManController EDIT page :
#model MKL.Models.ManProduct
<hr />
#Html.Partial("~/Views/Shared/_SearchProduct.cshtml")
<hr />
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
#Html.ValidationSummary(true)
#Html.HiddenFor(model => model.ManProductProductId)
#if (ViewBag.ProductId != null)
{
#Html.Hidden("ProductId", (int)ViewBag.ProductId)
}
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
SO need to dynamically set Partial view Controller name Man and Woman
#using (Html.BeginForm("Edit", "", FormMethod.Get)){}
You can pass a model to the partial controller:
#Html.Partial("~/Views/Shared/_SearchProduct.cshtml", Model)
In _SearchProduct.cshtml, Model will be of type WomanProduct or ManProduct, depending on the view that called Partial. Then, choose controller depending on the model type:
#{
var ctrl = Model is WomanProduct ? "Woman" : "Man";
}
#using (Html.BeginForm("Edit", ctrl, FormMethod.Get))
Well, I would suggest you to use #Html.Action to render your partialview. Consider below example.
#Html.Action("GetSearchPartial", "Controller", new {controller = "Women"})
//in the Women Edit View
#Html.Action("GetSearchPartial", "Controller", new {controller = "Man"})
//in the Man Edit View
Now write an action which returns ParialViewResult.
public PartialViewResult GetSearchPartial(string controller)
{
ViewBag.Controller=controller;
return PartialView("_SearchProduct");
}
in your _SearchProduct - PartialView get the ViewBag data and assign it as controller.
#{
string controller=ViewBag.Controller;
}
<div class="row">
<div class="col-md-6">
#using (Html.BeginForm("Edit", controller, FormMethod.Get))
{
<p>
Find by Product Id : #Html.TextBox("SearchString", ViewBag.CurrentFilter as string)
<input type="submit" value="Search" />
</p>
}
</div>
<div class="col-md-6">
<span style="color:green">#ViewBag.ProductName </span>
</div>
<span style="color:red"> #ViewBag.FindResult </span>
</div>
Let know if you face any issues
Try this way
#using (Html.BeginForm("Edit", #HttpContext.Current.Request.RequestContext.RouteData.Values["controller"].ToString(), FormMethod.Get)){}
instead of the actual location of the partial view. so dynamically set Man or Woman controller name

How to use 2 buttons on the same asp.net mvc form

In my asp.net mvc form I have 2 buttons, one to save which will save data from the from in a list in sharepoint and the second button does the same and additionally it applies some css colors.
I doubt however how to use 2 actions on the same form (same controller)
this is my view
#{
Layout = "~/Views/Shared/_LayoutPage2.cshtml";
}
#using (Html.BeginForm("Index", "Movies", FormMethod.Post))
{
<div class="row">
<div class="col-md-8">
<div class="col-xs-6 col-sm-3" id="stylesheet">Hojas de estilos</div>
<div class="col-xs-6 col-sm-3">
#Html.DropDownList("cssFiles", (IEnumerable<SelectListItem>)ViewBag.cssFiles, "Crear Nuevo", new { #class = "form-control", #id = "selCssFile" })
<span>
<input type="text" class="form-control" id="txtFileName" style="display:none;" placeholder="Nombre del archivo">
</span>
</div>
</div>
</div>
<div class="row">
<div class="col-md-8">
Color base, links, botones, borde encabezado y pie
</div>
<div class="col-md-4">
<div id="colorSelector" class="colorSelector"><div style="background-color: #0000ff"></div></div>
</div>
</div>
<div class="row">
<div class="col-md-8">
Fondo de la pagina, fondo de los cuadros
</div>
<div class="col-md-4">
<div id="colorSelector2" class="colorSelector"><div style="background-color: #0000ff"></div></div>
</div>
</div>
<div class="row">
<div class="col-md-8">
Navegación
</div>
<div class="col-md-4">
<div id="colorSelector3" class="colorSelector"><div style="background-color: #0000ff"></div></div>
</div>
</div>
<div class="row">
<div class="col-md-8">
Navegación (Item seleccionado)
</div>
<div class="col-md-4">
<div id="colorSelector4" class="colorSelector"><div style="background-color: #0000ff"></div></div>
</div>
</div>
<div class="row">
<div class="col-md-8">
Pie de página
</div>
<div class="col-md-4">
<div id="colorSelector5" class="colorSelector"><div style="background-color: #0000ff"></div></div>
</div>
</div>
<div class="row" id="buttons">
<div class="col-md-8">
</div>
<div class="col-md-4">
<button type="button" class="btn btn-success">Guardar</button>
<button type="button" class="btn btn-primary">Guardar y aplicar</button>
</div>
</div>
}
My index action on the customize controller so far
public class CustomizeController : Controller
{
// GET: Customize
public ActionResult Index()
{
User spUser = null;
var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext);
using (var cc = spContext.CreateUserClientContextForSPHost())
{
int aprovisionado = (int)cc.Web.GetPropertyBagValueInt("Vinculosc.PlantillasIntranet.Aprovisionado", 0);
if (aprovisionado == 0)
{
string libraryName = "ConfiguraciónColores";
Dictionary<string, string> fields = new Dictionary<string, string>();
fields.Add("Color1", "Text");
fields.Add("Color2", "Text");
fields.Add("Color3", "Text");
fields.Add("Color4", "Text");
fields.Add("Color5", "Text");
//ProvisionTemplate(cc);
CreateLibrary(cc, libraryName);
AddFields(cc, libraryName, fields);
}
}
#region comments
/*Uri hostWeb = new Uri(Request.QueryString["SPHostURL"]);
using (var clientContext = TokenHelper.GetS2SClientContextWithWindowsIdentity(hostWeb, Request.LogonUserIdentity))
{
var web = clientContext.Web;
clientContext.Load(web, w => w.Lists.Include(l => l.Title).Where(l => !l.hidden));
clientContext.ExecuteQuery();
return View(web.Lists);
}*/
#endregion
return View();
}
You can put the same name in the view with different value
<button type="submit" name="Guardar" value="guardar" class="btn btn-success">Guardar</button>
<button type="submit" name="Guardar" value="aplicar" class="btn btn-primary">Guardar y aplicar</button>
And in the Controller you can check the value of the button
if (Request["Guardar"].ToString() == "guardar")
{
//Your code for the first button
}
else
{
//Your code for the second button
}
Your button type should be "submit", and you can give them a name... Same name, that can be reused in a model, or by Request.Form["GiveAName"]
Your controller should have a
[HttpPost]
public ActionResult Index()
{
... Your code to retrieve form values
}
Anyway that's bad coding... You should work with models to inject on the view, that same model could be retrieved back and so you don't have to worry about retrieving form values. :=)

View is sending a null list of objects

I'm having a problem with a null list of objects when I try send from view to controller,
the jobOffertModel parameter is null
Here is my controller method: (I know it doesn't nothing, but I was testing the parameters)
[HttpPost]
public ActionResult AcceptJobOfferts(IEnumerable<JobOffertModel> jobOffertModel)
{
initBusinessObjects();
return View();
}
And Here is my View:
#model IEnumerable<TCCApplication.Models.JobOffertModel>
#{
ViewBag.Title = "Minhas ofertas de trabalho";
}
<h2>#ViewBag.Title</h2>
#using (Html.BeginForm("AcceptJobOfferts","Professional")){
<div class="row">
<div class="col-md-9">
#foreach (var item in Model)
{
<div class="row">
<div class="col-md-2">
<label>Oferta: </label>
#Html.DisplayFor(itemModel => item.Description)
</div>
#if (item.Acepted)
{
<div class="col-md-2">
<label>Aceitar?</label>
#Html.DisplayFor(itemModel => item.Acepted)
</div>
}
else
{
<div class="col-md-2">
<label>Aceitar?</label>
#Html.EditorFor(itemModel => item.Acepted)
</div>
}
<div class="col-md-2">
<label>Ativa</label>
#Html.DisplayFor(itemModel => item.Active)
</div>
<div class="col-md-3">
<label>Data do trabalho</label>
#Html.DisplayFor(itemModel => item.JobDate)
</div>
</div>
}
</div>
<div class="col-md-2">
<input type="submit" value="Aceitar ofertas de trabalho"/>
</div>
</div>
}
The user goes to view using this method:
public ActionResult ViewMyJobOfferts(int professionalId)
{
initBusinessObjects();
var professionalJobOfferts = jobOffertBusiness.GetJobOffertsByProfessional(professionalId);
return View(professionalJobOfferts);
}
Changes what I have done -
Convert IEnumerable to List() with variable called modelList.
Used For Loop to display all property values.
Used hidden fields to persist only display element properties for POST operation.
Changed parameter name of POST Controller Action to modelList.
You got to have your view something like this -
#model IEnumerable<TCCApplication.Models.JobOffertModel>
#{
ViewBag.Title = "Minhas ofertas de trabalho";
}
<h2>#ViewBag.Title</h2>
#using (Html.BeginForm("AcceptJobOfferts","Professional", FormMethod.Post)){
var modelList = Model.ToList();
<div class="row">
<div class="col-md-9">
for (int i = 0; i < modelList.Count; i++)
{
<div class="row">
<div class="col-md-2">
<label>Oferta: </label>
#Html.DisplayFor(itemModel => modelList[i].Description)
#Html.HiddenFor(itemModel => modelList[i].Description)
</div>
#if (modelList[i].Acepted)
{
<div class="col-md-2">
<label>Aceitar?</label>
#Html.DisplayFor(itemModel => modelList[i].Acepted)
#Html.HiddenFor(itemModel => modelList[i].Acepted)
</div>
}
else
{
<div class="col-md-2">
<label>Aceitar?</label>
#Html.EditorFor(itemModel => modelList[i].Acepted)
</div>
}
<div class="col-md-2">
<label>Ativa</label>
#Html.DisplayFor(itemModel => modelList[i].Active)
#Html.HiddenFor(itemModel => modelList[i].Active)
</div>
<div class="col-md-3">
<label>Data do trabalho</label>
#Html.DisplayFor(itemModel => modelList[i].JobDate)
#Html.HiddenFor(itemModel => modelList[i].JobDate)
</div>
</div>
}
</div>
<div class="col-md-2">
<input type="submit" value="Aceitar ofertas de trabalho"/>
</div>
</div>
}
And then your controller should be -
[HttpPost]
public ActionResult AcceptJobOfferts(IEnumerable<JobOffertModel> modelList)
{
initBusinessObjects();
return View();
}
Replace this line :
#using (Html.BeginForm("AcceptJobOfferts","Professional")){
By:
#using (Html.BeginForm("AcceptJobOfferts","Professional", FormMethod.Post)){

MVC Retrieve Multi Entry from View in Controller

this is my controller:
public ActionResult Create() {
Number newNumber = new Number();
return View(newNumber);
}
and View :
#model PhoneBook.Models.Number
#{
ViewBag.Title = "Create";
}
<h2>Create</h2>
<script src="../../Scripts/jQuery.AddNewNumber.js" type="text/javascript"></script>
#using (Html.BeginForm()) {
#Html.ValidationSummary(true)
#Html.HiddenFor(model => model.Contact.Id)
<fieldset>
<legend>Number</legend>
<div class="TargetElements">
<div class="editor-label">
#Html.LabelFor(model => model.PhoneNumber)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.PhoneNumber)
#Html.ValidationMessageFor(model => model.PhoneNumber)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.NumberKind)
</div>
<div class="editor-field">
#Html.DropDownListFor(model => model.NumberKind.Title, NumberKinds)
</div>
</div>
<p>
<input class="AddNew" value="Add New" type="button" /></p>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
By press AddNew button (use jQuery AddNewNumber.js) new input+DropDown Added to form in client side. But there is a problem in retrieve values, When I have one entry element(include input+DropDown) I can retrieve values like the following in post of my controller:
[HttpPost]
public ActionResult Create(Number NewNumber, FormCollection collection) {
db.Numbers.Add(NewNumber);
db.SaveChanges();
return RedirectToAction("Index")
}
But when there is multi-entry how can I retrieve values and add them to DataBase?
Use by the name of element like this:
string[] PhoneNumbers = collection.GetValues("PhoneNumber");
You want all of your input elements to have the same name. Then in your POST action method, the parameter would be List. Here is an example:
Model Binding to a List of objects

Categories

Resources