ASP MVC EF MultiSelect HTTP POST - c#

My Controller:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(BlogCompModel blogmodel)
{
if (ModelState.IsValid)
{
...
}
}
My View:
#model BlogProject.Models.BlogCompModel
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
...
<div class="form-group">
#Html.LabelFor(model => model.BlogCompModel.posts, "Property", new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.ListBoxFor(model => model.posts, new MultiSelectList(Model.posts, "post_ID", "postTitle"))
#Html.ValidationMessageFor(model => model.posts.posts)
</div>
</div>
}
Error message on post:
"The parameter conversion from type 'System.String' to type 'BlogProject.Models.Posts' failed because no type converter can convert between these types."} System.Exception {System.InvalidOperationException}
As you can see, I'm not sure how to translate the HTML Multiselect list from a collection of Post_IDs to an ICollection of posts.
Thanks!

You can add another property in your BlogCompModel Model, it'll wrap all selected post in it.
public class BlogCompModel
{
//
public string[] selectedPosts { get; set; }
}
Then in your view:
#Html.ListBoxFor(model => model.selectedPosts ,
new MultiSelectList(Model.posts, "post_ID", "postTitle"))
#Html.ValidationMessageFor(model => model.selectedPosts )

In my blog MVC app, I've a multiselect for Tags and here how I add those to Post in Create action.
Index.cshtml
#model MyBlog.Core.Post
#Html.ListBox("PostTags", (MultiSelectList)ViewBag.MultiSelectList, new { #class = "form-control" })
controller
public ActionResult CreatePost([Bind(Include = "Id,Title,ShortDescription,Description,Published,PostedOn,ModifiedOn,CategoryId")] Post post, int[] postTags)
{
if (postTags != null)
{
foreach (var t in postTags)
{
var tag = _dbTag.GetById(t);
post.Tags.Add(tag);
}
}
// save to database and other stuff
return View(post);
}

Related

Routing is not going to the post part of the controller

I can´t get into the HttpPost part of the controller.
I´ve created another controller and the problem is the same
I was working fine before
I´ve added tags [Route("CrearSolicitud")]
[HttpPost, ActionName("CrearSolicitud")]
I´ve haven´t added anything to the routing part
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
It goes to http://localhost:55935/Solicitudes
instead of http://localhost:55935/Solicitudes/CrearSolicitud
public class SolicitudesController : Controller
{
ApplicationDbContext db = new ApplicationDbContext();
// GET: Solicitudes
public ActionResult Index()
{
return View();
}
//Get
public ActionResult Solicitud()
{
return View();
}
//Post
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Solicitud(Solicitudes s)
{
s.Id = "e17cba68-0a0b-4d6e-abaf-8026cb91fcd1";
s.fk_tipo_transaccion = 3;
s.fk_estado_solicitud = 1;
db.Solicitudes.Add(s);
db.SaveChanges();
return View();
}
The view
#model HGRecursosHumanos4.Models.Solicitudes
#{
Layout = "~/Views/Shared/_Layout.cshtml";
}
#using (#Html.BeginForm("CrearSolicitud", "Solicitudes", FormMethod.Post))
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="container">
<div class="form-group col-md-5">
#Html.LabelFor(model => model.VacacionesDias, htmlAttributes: new { #class = "control-label col-md-4" })
<div class="col-md-3">
#Html.EditorFor(model => model.VacacionesDias, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.VacacionesDias, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group col-md-5">
#Html.LabelFor(model => model.FechaInicio, htmlAttributes: new { #class = "control-label col-md-3" })
<div class="col-md-5">
<input type="date" id="fechaInicio" name="fechaInicio" class="Filtros form-control input-sm" />
</div>
</div>
<div class="form-group col-md-2">
<div class="col-md-offset-2 col-md-3">
<input type="submit" value="Solicitar" class="btn btn-success" />
</div>
</div>
</div>
</div>
}
When I use the form and hit the submit button or the application goes to Index or to the same view again
It´s like EF went broke or something
I´ve added that, an error comes out The 'CrearSolicitud' view or its master view is not found or there is no search engine that supports the search locations. We searched in the following locations:
~/Views/Solicitudes/CrearSolicitud.aspx
~/Views/Solicitudes/CrearSolicitud.ascx
~/Views/Shared/CrearSolicitud.aspx
~/Views/Shared/CrearSolicitud.ascx
~/Views/Solicitudes/CrearSolicitud.cshtml
~/Views/Solicitudes/CrearSolicitud.vbhtml
~/Views/Shared/CrearSolicitud.cshtml
~/Views/Shared/CrearSolicitud.vbhtml
#Nemanja I certainly don´t have that view, if I use the same name on the post and no the get, the application goes to the get part again
I have the solution.
The thing is, index is for showing a list of added registries, when it is changed for something else you can´t see that list and that leads you to wrong conclusions.
The routing is working fine, the program is doing what is supposed to do, the RedirectToAction("Index") is ok.
The lesson is, take a while and think about what are you changing and try to follow the VS way of doing things.
I´ve tested now and it´s working
ApplicationDbContext db = new ApplicationDbContext();
// GET: Solicitudes
public ActionResult Index()
{
return View();
}
public ActionResult Index2()
{
return View(db.Solicitudes.ToList());
}
//Get
public ActionResult Solicitud()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Solicitud(Solicitudes s)
{
try
{
if (ModelState.IsValid)
{
s.FechaFinal = DateTime.Now;
s.FechaYHoraSolicitud = DateTime.Now;
s.Id = "e17cba68-0a0b-4d6e-abaf-8026cb91fcd1";
s.fk_tipo_transaccion = 3;
s.fk_estado_solicitud = 1;
db.Solicitudes.Add(s);
db.SaveChanges();
ViewBag.Message = "Solicitud guardada";
ModelState.Clear();
return RedirectToAction("Index2");
}
return View("ModelStateError");
}
//catch
catch (Exception ex)
{
//throw ex;
Console.WriteLine(ex.Message);
return View("Error");
}
}

I'm losing data on the way Controller-View-Controller. How right get Model from View?

I have the Model which contains the few properties. There are List<Point> and ThranslatingCam. The ThranslatingCam contains property which should be entered by the user.
public class TranslatingCamModel
{
public TranslatingCam Cam { get; set; }
public List<Point> Points { get; set; }
}
Now I use the Points just for visualization but in future this list will be changed by user.
In the Controller I initialize the Points list, add it to my TranslatingCamModel and send it to the View.
public ActionResult Cam()
{
TranslatingCamModel model = new TranslatingCamModel();
List<Points> points = new List<Points>();
//here I add same data to the points list
model.Points = points;
model.Cam = new TranslatingCam();
return View("TranslatingCam", model);
}
This code works as I want. My problem is further.
#model CamShaft.WebUI.Models.TranslatingCamModel
#{
ViewBag.Title = "Cam";
}
<div class="prop">
#using (Html.BeginForm("DrawTranslatingCam", "Home"))
{
#Html.LabelFor(model => model.Cam.Rmin, htmlAttributes: new { #class = "control-label col-md-2" })
#Html.EditorFor(model => model.Cam.Rmin, new { htmlAttributes = new { #class = "form-control" } })
//And the same for the other Cam's properties
<input type="submit" value="Draw" />
}
</div>
//Here is my code for visualization and etc... And here I can work with the model.Points.
It works okay too, I get value for Cam but I lose my Points list.
public ActionResult DrawTranslatingCam(TranslatingCamModel model)
{
//Here I have model returned from the View
}
Here is model.Cam with entered by user data but model.Points list is null. But I initialized it before I sent it to the View..
I tried to solve using #Html.HiddenFor:
#using (Html.BeginForm("DrawTranslatingCam", "Home"))
{
#Html.LabelFor(model => model.Cam.Rmin, htmlAttributes: new { #class = "control-label col-md-2" })
#Html.EditorFor(model => model.Cam.Rmin, new { htmlAttributes = new { #class = "form-control" } })
//And the same for the other Cam's properties
#Html.HiddenFor(model => model.Points)
<input type="submit" value="Draw" />
}
In this case is returned Points list but without date (not null, just with count = 0)
So, how right I can get my Model from the View?
The code has been slightly modified for compactness and clarity.

Unable to edit IdentityRole

I have the following view:
#model Microsoft.AspNet.Identity.EntityFramework.IdentityRole
#{
ViewBag.Title = Resources.Edit;
}
<h2>#Resources.EditRole</h2>
#Html.ActionLink(Resources.ListRoles, "Index") | #Html.ActionLink(Resources.ManageUserRoles, "ManageUserRoles")
<hr />
<div class="row">
<div class="col-md-8">
<section id="editRoleForm">
#using (Html.BeginForm("Edit", "Role", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { #class = "form-horizontal", role = "form" }))
{
#Html.AntiForgeryToken()
<h4>#Resources.Role</h4>
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
#Html.HiddenFor(model => model.Id)
<div class="form-group">
#Html.LabelFor(m => m.Name, new { #class = "col-md-2 control-label" })
<div class="col-md-10">
#Html.TextBoxFor(m => m.Name, new { #class = "form-control" })
#Html.ValidationMessageFor(m => m.Name, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="#Resources.Save" class="btn btn-default" />
</div>
</div>
}
</section>
</div>
</div>
I also have the following two methods in my RoleController:
//
// GET: /Role/Edit/5
public ActionResult Edit(string Role)
{
var thisRole = context.Roles.Where(r => r.Name.Equals(Role, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
return View(thisRole);
}
//
// POST: /Role/Edit/5
[HttpPost]
public ActionResult Edit(FormCollection collection)
{
try
{
var thisRole = context.Roles.Where(r => r.Id.Equals(collection["Id"])).FirstOrDefault();
thisRole.Name = collection["Name"];
context.SaveChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
Originally, I was trying to use this method instead of the second one:
//
// POST: /Role/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(IdentityRole Name)
{
try
{
context.Entry(Name).State = System.Data.Entity.EntityState.Modified;
context.SaveChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
But I never got it to work because the Name parameter was always null -which I still don't know why it happened, so if someone can explain it to me that'll be greatly appreciated.
I wrote then the other method since I saw the use of FormCollection in another example (to create roles) and it seems to work fine, at least it contains the information I need when I debug it. My issue is that although collection["id"] has the right Id for the Role I'm trying to edit, context.Roles is completely empty. This makes no sense to me given that when the first method is called (loading the View for the first time), this line
var thisRole = context.Roles.Where(r => r.Name.Equals(Role, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
returns the selected role (out of the several that exist and that I can see when I add to watch context.Roles). However, after the view is loaded, the textbox edited and the second method in the controller gets called, context.Roles has nothing in it. Why?
Ok you can use the already built in [Authorize(Roles="RoleType")] filter.
You then have your regular User model and Account controller so you can then authorize users. Once you have authorised them you can set them to a specific role.
E.g. user story: only admins can access action result x
[Authorize(User="Admin")]
Public ActionResult X(){
...
}
That way you simply assign user roles in the model creation.
E.g.
Public Class UserModel
{
int id {get;set;}
string name {get;set;}
string Role {get;set;}
.....
}
Now only users that have been authorised AND are of Role type "Admin" will be able to access that controller.
If you want to edit their role you can do a simple edit user action method
e.g.
[Post]
public actionresult edituser(int? id)
{
if (id == null)
{
return HttpNotFound();
}
using (var db = new UserContext())
{
UserModel editUser = db.UserModel.Find(id);
if (editUser == null)
{
return HttpNotFound();
}
db.User(editModel).State = System.Data.Entity.EntityState.Modified;
db.SaveChanges();
}
RedirectToAction("Action", "Controller");
}
Now any user that is NOT of Role type "Admin" will not be able to access that screen. They will receive a 404 error.

Partial views and html forms Asp Mvc

I am attempting to render a partial view that contains a simple html form. I want to render the form from a controller as I handle the postback from an overloaded controller method. I have tried #Html.Action("ContactForm")but I get an Exception because child actions cannot redirect.
My Controller:
public ActionResult Index()
{
return View();
}
[HttpGet]
public ActionResult ContactForm()
{
return PartialView(new ContactForm());
}
[HttpPost]
public ActionResult ContactForm(ContactForm Contact)
{
return RedirectToAction("FormResults", new { ContactForm = Contact });
}
public ActionResult FormResults(ContactForm Contact)
{
return PartialView(Contact);
}
My Form:
#using(Html.BeginForm())
{
<h2>Contact Form</h2>
<div class="input-group">
<h4>#Html.LabelFor(m => m.FirstName, "First Name")</h4>
#Html.TextBoxFor(m => m.FirstName, new { #class = "form-control", #placeholder = "First Name" })
</div>
<div class="input-group">
<h4>#Html.LabelFor(m => m.LastName, "Last Name")</h4>
#Html.TextBoxFor(m => m.LastName, new { #class = "form-control", #placeholder = "Last Name" })
</div>
<div class="input-group">
<h4>#Html.LabelFor(m => m.Email, "Email")</h4>
#Html.TextBoxFor(m => m.Email, new { #class = "form-control", #placeholder = "Email", #type = "text" })
</div>
<input type="submit" class="btn btn-info" value="Submit" />
}
Any Help on how I would accomplish this would be appreciated.
try surrounding the form with a div and a certain id and use:
#using(Ajax.BeginForm("ContactForm","YourController",new AjaxOptions()
{
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "yourCertainId",
HTTPMethod = "POST"
})
and your ActionMethod:
[HttpPost]
public ActionResult ContactForm(ContactForm Contact)
{
return Partial("YourPartialName", Contact });
}
make sure that you include the bundle jqueryval on the bottom of your view.
you wont need the second controller method "FormResults"
Does something like this not work for you?
I don't think you need a redirect.
[HttpPost]
public ActionResult ContactForm(ContactForm Contact)
{
return PartialView("FormResults", Contact);
}
This uses the
PartialView(string viewName, object model)
overload of the PartialView method in the Controller class.
This allows you to use a View that doesn't match the ActionResult's method name.
The same thing works for the plain "View" method as well.

asp.net view syntax and html helper

This is actually two questions in one.
First question is about the following:
<div class="form-group">
#Html.LabelFor(model => model.xxx, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.xxx , new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.xxx, "", new { #class = "text-danger" })
</div>
</div>
I don't understand what model => model.xxx means, I know what it's doing but i don't know how to interpret the syntax.
the second question is, if I have - for example -
foreach (var item in Model)
how can I replace
#Html.EditorFor(model => model.xxx , new { htmlAttributes = new { #class = "form-control" } })
with
#Html.EditorFor(item.stringProperty , new { htmlAttributes = new { #class = "form-control" } })
when I try this, it gives me errors, is there an overloaded EditorFor helper that accepts this?
thank you!
One view can has 0 or 1 Model, which's sending from controller.
public class Person
{
public string Name {get;set;}
public int Age {get;set;}
}
public ViewResult Index()
{
Person p = new Person() { Name = "P1", Age = 100};
return View(p);//
}
if your View's name "Index" then you can use second way for View, which contain 2 parameters:
ViewName and model
return View("Index", model: p);
then in your View you can use the model, if it has been implemented that:
#model Person//and remember about namespace
#
{
...
}
#using(Html.BeginForm("ActionName", "controllerName", FormMethod.Post))
{
#Html.EditorFor(model => model.Name); // it create input, check in F12 in your browse - then you can exactly understand.
}
if you want create Editor for item you must use:
#Html.TextBox("YourName")
for example:
#using(Html.BeginForm("Action", "controller")
{
#Html.TextBox("YourName")
<input type="submit" value="ok"/>
}
and in your controllerController:
public ViewResult Action(string YourName)
{
//here you got value for string YourName
return View();
}
and helpfully answer's here:
ASP.NET MVC get textbox input value
Edit, answer about exactly problem (which containt in comment below question):
I have a list, and I want to display an input text box for each item in the list, but I want each text box to have text inside when its created, text from each item in the list (coming from the item's property)
#foreach(var item in Model)
#using(Html.BeginForm("MyMethod", "Controller"))
{
#Html.TextBox("item", item)
<input type="submit" value="ok" />
}
and in your controller add MyMethod:
[HttpPost]
public ViewResult MyMethod(string item)
{
...
}
or
[HttpPost]
public ViewResult MyMethod(int item) //if it's an int
{
...
}
and if you want to have a better security page please read about Html.AntiForgeryToken:
http://msdn.microsoft.com/en-us/library/dd470175(v=vs.118).aspx
#using(Html...())
{
#Html.AntiForgeryToken()
(...)
}
I see you already got the answer of your first question.
For the second one, i think
#Html.EditorFor(item => item.stringProperty , new { htmlAttributes = new { #class = "form-control" } })
will work fine

Categories