Parameter passing in an MVC Controller - asp.net-mvc

I have the following ActionResult in my controller
[HttpGetAttribute]
public ActionResult _UpdateAlertNote(int recordId)
{
DealActionUpdateAlertNoteViewModel vm = new DealActionUpdateAlertNoteViewModel();
dtDeal_v10_r1.Manager objMan = new dtDeal_v10_r1.Manager(ref mobjSecurity);
dtDeal_v10_r1.Deal objDeal = default(dtDeal_v10_r1.Deal);
objDeal = objMan.GetDealObject(recordId, true);
vm.Message = objDeal.AlertMessage;
vm.IsDefaultStyle = objDeal.Alert_UseDefaultStyle;
vm.BackgroundColor = objDeal.Alert_BackgroundColor;
vm.FontColor = objDeal.Alert_FontColor;
vm.DealId = recordId;
return PartialView(vm);
}
Also the following ActionResult
[HttpPost]
public ActionResult _UpdateAlertNote(DealActionUpdateAlertNoteViewModel vm)
{
dtDeal_v10_r1.Manager objMan = new dtDeal_v10_r1.Manager(ref mobjSecurity);
objMan.UpdateAlertMessage(vm.DealId, vm.Message, vm.IsDefaultStyle, vm.FontColor, vm.BackgroundColor);
return this.PartialView("_action", vm.DealId);
}
When I execute this code it the "DealId" comes up as 0 in the Post.
I checked the Get and the DealId is being stored in the vm.DealId but is not passed through to the Post method.
I am not sure why it isn't passing could someone help me out with this.
****EDIT json added***
DealerSocket.TakeAction.updateDealAlertNote = function () {
var controller = "/DealAction/_UpdateAlertNote?mDeal_ID=";
var formId = "_UpdateDealAlertNoteFormElement";
DealerSocket.TakeAction.PostActionAndRefresh(formId, controller);
};

When you are posting an HTML Form element to a POST action, you need to make sure the value you pass to the view is stored in a Form element.
In this case of yours you will need something like:
<input type="hidden" value="#vm.DealId" />
within the <form> tag that you are Posting to the action.
Only elements inside the <form> tag will be serialized and sent to the Action.

Related

Reference DropDownList selected value from enclosing Form

I'm just getting started with MVC5 (from WebForms), and dropdownlist bindings are giving me some fits.
I'd like to get this working using a GET request back to the page, with a selected value parameter. I'm hopeful that I can specify the route arguments in the form itself, so I'd like to reference the DDL's SelectedValue.
<p>
#using (Html.BeginForm("Index", "Profile", FormMethod.Get, new { id = WHATDOIPUTHERE} )) {
#Html.AntiForgeryToken()
#Html.DropDownList("ApplicationID", new SelectList(ViewBag.ApplicationList, "ApplicationID", "ApplicationName", ViewBag.SelectedApplicationId), new {onchange = "this.form.submit();"})
}
</p>
I can make it work with a POST form, but that requires a second controller method so I end up with
public ActionResult Index(long? id) {
ConfigManager config = new ConfigManager();
//handle application. default to the first application returned if none is supplied.
ViewBag.ApplicationList = config.GetApplications().ToList();
if (id != null) {
ViewBag.SelectedApplicationId = (long)id;
}
else {
ViewBag.SelectedApplicationId = ViewBag.ApplicationList[0].ApplicationID; //just a safe default, if no param provided.
}
//handle profile list.
List<ProfileViewModel> ps = new List<ProfileViewModel>();
ps = (from p in config.GetProfilesByApp((long)ViewBag.SelectedApplicationId) select new ProfileViewModel(p)).ToList();
return View(ps);
}
//POST: Profile
//read the form post result, and recall Index, passing in the ID.
[HttpPost]
public ActionResult index(FormCollection collection) {
return RedirectToAction("Index", "Profile", new {id = collection["ApplicationId"]});
}
It would be really nice to get rid of the POST method, since this View only ever lists child entities.
What do you think?
You can update your GET action method parameter name to be same as your dropdown name.
I also made some small changes to avoid possible null reference exceptions.
public ActionResult Index(long? ApplicationID) {
var config = new ConfigManager();
var applicationList = config.GetApplications().ToList();
ViewBag.ApplicationList = applicationList ;
if (ApplicationID!= null) {
ViewBag.SelectedApplicationId = ApplicationID.Value;
}
else
{
if(applicationList.Any())
{
ViewBag.SelectedApplicationId = applicationList[0].ApplicationID;
}
}
var ps = new List<ProfileViewModel>();
ps = (from p in config.GetProfilesByApp((long)ViewBag.SelectedApplicationId)
select new ProfileViewModel(p)).ToList();
return View(ps);
}

Postback for the second time from asp.net MVC view

Setup:
I am working on reset password page and planning to have a single view to display three forms one after another.
Initially user will be asked to enter username, then the form gets submitted.
I am able to capture it is HttpPost action method.
I am returning the same view but this time I am rendering a different form, I am doing this using Razor
When I try to submit (calling a different action method) from the second form that I am displaying now I am getting and error saying that Resource cannot be found
Question:
Is it not possible to post back twice from the sample page/view?
If possible how can I achieve it?
CSHTML:
#if (ViewBag.step == 1) {
<form id="frmUsername" action="#Url.Content("~/Account/ValidateAnswers")" method="post" novalidate="novalidate" ng-init="initStep1()">
...
</form>
}
#if (ViewBag.step == 2) {
<form action="#Url.Content("~/Account/ValidateUsername")" id="frmQuestions" ng-init="initStep2()" novalidate="novalidate">
...
</form>
}
Controller methods:
public ActionResult RecoverPassword(string userType)
{
ViewBag.step = 1;
ViewBag.userType = userType;
ViewBag.showUsernameErrorMessage = "false";
return View();
}
[AllowAnonymous]
[HttpPost]
public ActionResult ValidateUsername(string userName, string selectedUserType)
{
...
ViewBag.step = responseStep;
ViewBag.showUsernameErrorMessage = responseShowUsernameErrorMessage;
ViewBag.userName = userName;
ViewBag.userType = selectedUserType;
return View("~/Views/Account/RecoverPassword.cshtml");
}
[HttpPost]
public ActionResult ValidateAnswers(string answer1, string answer2, string answer3, string questionId1, string questionId2, string questionId3, string step, string userName, string selectedUserType)
{
...
ViewBag.step = responseStep;
ViewBag.showUsernameErrorMessage = responseShowUsernameErrorMessage;
ViewBag.userName = userName;
ViewBag.userType = selectedUserType;
return View("~/Views/Account/RecoverPassword.cshtml");
}

from data to controller and passing it to form

I'm doing my first steps in mvc and I need help.
I'm passing data from view to this controller and I need to pass the selected items with there details to a different view (that is a form that the user add his email details) and I cant figure out how to .
This is how I'm getting the details to the controller from the submitted form
public ActionResult list()
{
var AllItems = db.menu.ToList();
Mapper.CreateMap<Menu, SelectableMenu>();
return View(AllItems.Select(m => new SelectableMenu { price = m.price, MenuId = m.MenuId, Name = m.Name })
.ToList());
}
[HttpPost]
public ActionResult List(IEnumerable<SelectableMenu> item)
{
var userSelectedMenu = item.Where(m => m.IsSelected).Select(m => m.Name + m.price + m.MenuId);
if (userSelectedMenu != null && userSelectedMenu.Any())
{
return View("bla");
}
return View();
}
Use method ReditectToActionstring actionName, string controllerName, Object routeValues)
for details go to: http://msdn.microsoft.com/en-us/library/dd460311(v=vs.108).aspx
You can return different view using return View("ViewName",model)
For eg:
[HttpPost]
public ActionResult List(IEnumerable<SelectableMenu> item)
{
var userSelectedMenu = item.Where(m => m.IsSelected).Select(m => m.Name + m.price + m.MenuId);
if (userSelectedMenu != null && userSelectedMenu.Any())
{
return View("YourDiffrentViewName",userSelectedMenu); // This will pass your model to your Different view
}
return View();
}
Then in your new view you will have to strongly typed it with your model.
For eg :
Your view will be as follows:
#model ProjectName.models.YourClassName //Your class/model namespace
#using(Html.BeginForm())
{
#Html.TextBoxFor(m => Model.Property) //This will create textbox for your property
<input type="submit" value="Submit" />
}
For more on stronly typed views visit:
http://www.c-sharpcorner.com/UploadFile/abhikumarvatsa/strongly-typed-views-in-mvc/
http://www.howmvcworks.net/OnViews/BuildingAStronglyTypedView
You will need twosteps for this
Step 1
Make a model(it is more effective) use it in a view to pass your data to controller through post in submission of form.
Step 2
Receive the data into the controller method then use
return View("yourNewpage","yourdatamodelobject"); in the controller action to pass the data in the action result view of another page.
Alternatively, if the view is in another controller
then you can receive data here in the post action method and use Return RedirectToAction("ActionName", "ControllerName", "DataModelObject") to pass to a diffrent controller

How do I upload an image file using MVC2

Im trying to us a controller in MVC2 to upload a file but i get an object reference not set to an instance of an object error
[HttpPost]
public ActionResult AddPhoto(int id, FormCollection formValues, HttpPostedFile image, AlbumPhotos photo )
{
AlbumPhoto photos = new AlbumPhoto();
UserPhotoAlbum album = AlbumRepo.GetAlbum(id);
photo.AlbumId = id;
photos.AlbumId = photo.AlbumId;
photo.PostedDate = DateTime.Now;
photos.PostedDate = photo.PostedDate;
album.LastUpdate = photo.PostedDate;
if (image.FileName != null)
{
photo.PhotoMimeType = image.ContentType;
photos.PhotoMimeType = image.ContentType;
photo.PhotoData = new byte[image.ContentLength];
photos.PhotoData = photo.PhotoData;
image.InputStream.Read(photo.PhotoData, 0, image.ContentLength);
photo.PhotoUrl = "../../Content/UserPhotos/" + image.FileName;
photos.PhotoUrl = photo.PhotoUrl;
image.SaveAs(Server.MapPath("~/Content/UserPhotos/" + image.FileName));
}
AlbumRepo.AddPhotoToAlbum(photos);
AlbumRepo.Save();
return RedirectToAction("Album", new { id = photo.AlbumId });
}
Please tell me if i'm doing something wrong.
You need to ensure the <form> tag has a enctype="multipart/form-data" value in the object htmlAttribute paramter of Html.BeginForm()
The name of the input file control should match the name of the parameter passed into the [HttpPost] Action method.
e.g.
<input type="file" name="uploadControl" />
should match 'name-wise' to:
public ActionResult AddPhoto(int id, FormCollection formValues, HttpPostedFile uploadControl, AlbumPhotos photo)
See http://www.277hz.co.uk/Blog/Show/7/image-uploading-in-asp-net-mvc for a good explanation.

ASP.NET MVC: Server Validation & Keeping URL paramters when returning the view

I currently have the following code for the POST to edit a customer note.
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult EditNote(Note note)
{
if (ValidateNote(note))
{
_customerRepository.Save(note);
return RedirectToAction("Notes", "Customers", new { id = note.CustomerID.ToString() });
}
else
{
var _customer = _customerRepository.GetCustomer(new Customer() { CustomerID = Convert.ToInt32(note.CustomerID) });
var _notePriorities = _customerRepository.GetNotePriorities(new Paging(), new NotePriority() { NotePriorityActive = true });
IEnumerable<SelectListItem> _selectNotePriorities = from c in _notePriorities
select new SelectListItem
{
Text = c.NotePriorityName,
Value = c.NotePriorityID.ToString()
};
var viewState = new GenericViewState
{
Customer = _customer,
SelectNotePriorities = _selectNotePriorities
};
return View(viewState);
}
}
If Validation fails, I want it to render the EditNote view again but preserve the url parameters (NoteID and CustomerID) for something like this: "http://localhost:63137/Customers/EditNote/?NoteID=7&CustomerID=28"
Any ideas on how to accomplish this?
Thanks!
This action is hit by using a post. Wouldn't you want the params to come through as part of the form rather than in the url?
If you do want it, I suppose you could do a RedirectToAction to the edit GET action which contains the noteId and customerId. This would effectively make your action look like this:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult EditNote(Note note)
{
if (ValidateNote(note))
{
_customerRepository.Save(note);
return RedirectToAction("Notes", "Customers", new { id = note.CustomerID.ToString() });
}
//It's failed, so do a redirect to action. The EditNote action here would point to the original edit note url.
return RedirectToAction("EditNote", "Customers", new { id = note.CustomerID.ToString() });
}
The benefit of this is that you've removed the need to duplicate your code that gets the customer, notes and wotnot. The downside (although I can't see where it does it here) is that you're not returning validation failures.

Resources