After selecting an accountholder I want to prepopulate my Order Create View with the properties of the selected accountholder.
My Controller Action so far:
[HttpPost]
public ActionResult Create(FormCollection values)
{
var accountHolder = from a in unitOfWork.AccountHolderRepository.Get(includeProperties: "AccountHolder")
where a.CustSName == values["Name"]
select a;
foreach (var a in accountHolder)
{
ViewBag.CustFName = a.CustFName;
ViewBag.CustSName = values["Name"];
ViewBag.CustPhone = a.CustPhone;
ViewBag.CustEmail = a.CustEmail;
}
return RedirectToAction("Create", "Order");
}
Not sure if I understand correctly what are you trying to accomplish here. I'm assuming :
Displaying Empty Create form
User provides value for AccountHolder (automatic submit happens?)
You return pre-populated form
Final Create step to preserve values to database
Am I right ?
If so, instantiate the model / viewmodel you're using in your create form (you're using strongly typed view right ?) and return it like this :
return View(yourobject); //Assuming the first view returned by GET request to Create action has all the properties in place
however that should happen only if the values are missing, right. so you might want to add some more logic to your controller to verify if pre-popullation or db.Save() is required.
As you're calling RedirectToAction to the Order controller, I assume you are now in the Create method of the accountholderController?
What's your question exactly? Without specifics, I can't give you much help.
Some notes though:
Try to search for the accountholder based on its ID in the database rather than the name. You are now trusting your enduser to enter the accountholder name exactly as it is entered in the database (same case, same punctuation). ID's are more precise and require less effort to get right.
Why use the Post-Create method if all you want to do is select an accountholder from a list and then open a Create view? It would be much wiser to have a dropdownlist containing all accountholders on your main page (or wherever you want to put it). something along the lines of
<select name="accountholderID">
<option value:"ID_of_accountholder">Name_of_accountholder</option>
...
</select>
Add a button next to that. Once an accountholder is selected and the button is clicked, call your (Get, not Post) Create method in the OrderController. Pass the accountholderID as a parameter. Your Create methoud should be something like:
public ActionResult Create(string accountholderID)
{
int ID = Convert.ToInt32(accountholderID);
ViewData["Accountholder"] = database.tbl_Accountholders.SingleorDefault(x=> x.Id == ID);
...
And in your Create View just access the values of your accountholder like so:
<% var accountholder = (accountholdertype)ViewData["Accountholder"]; %>
<span> Name is <%: accountholder.Name %> </span>
I think that should get you where you want to be :-)
Related
I could not find the similar question yet, so I decided to ask it here.
I relatively new to MVC and may have some incorrect wording in my question, and I'm just wondering if that is possible to do it at all?
Usually, we are dealing with ModelViewController coupling and we return the View from a Controller with Models/Json as parameters to the returning View, so we can bind the Model to the View
I'm just wondering if we have a ViewA, ViewB ControllerA,ControllerB and a ModelA, is that possible to have a #Url.Action/Link or Ajax method to send the ModelA from the ViewA to an Action Method of a ControllerB, so, the data stored in the ModelA can be displayed in the ViewB when it is returned from the ControllerB?
I do not have any code yet and just want to know if that is possible and if so, what would be the right approach to achieve something like that?
You could do something like this:
Controller B:
public IActionResult ControllerB(ModelA data)
{
return View(data);
}
View A:
#foreach (var data in Model)
{
<li>#Html.ActionLink(#data.ModelAProperty, "ControllerB", "ControllerBFileName", new { id = data.Sys.Id })</li>
}
View B:
#model YourModel.Models.ModelA
<div>
<h3>#Model.ModelAProperty</h3>
<p>#Model.ModelAOtherProperty</p>
</div>
This should work I believe. I did this with a previous project but it was a slightly different set up, but I believe I have modified it correctly to fit your needs. Basically you are passing the data to the controller with the first view, and then using that controller to pass the data to the next view.
I am trying to populate a dropdown from another Controller. I have DeviceController that has a #Html.DropDownList that get populated. Originally I have the code that populates the DropDown in the controller. Now I am trying to move it to a Controller designated for dropdowns. So I have the code to populate the dropdown in one location instead of the same code in multiple Controllers. The dropdown loads correctly when I have it in the original controller however when I move it the DropDown controller I receive the following error There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'DropDownDeviceType'.
The code for my view -
#Html.DropDownList("DropDownDeviceType", null, htmlAttributes: new { #class = "form-control" })
From my DropDownController (If I have this code in my DeviceController it works as intended)
public void PopulateDevciceType(object selectDeviceType = null)
{
string voidInd = "N";
var deviceType = db.DeviceTypes
.Where(x => x.VoidInd == voidInd)
.OrderBy(x => x.DeviceType1);
//ViewData["DropDownDeviceType"] = new SelectList(deviceType, "DeviceTypeID", "DeviceType1", selectDeviceType);
ViewBag.DropDownDeviceType = new SelectList(deviceType, "DeviceTypeID", "DeviceType1", selectDeviceType);
}
In my DeviceController
//Populate dropdowns
DropDownController dropDown = new DropDownController();
dropDown.PopulateDevciceType(model.DeviceTypeID);
I understand your desire to farm out certain tasks to dedicated classes. However, I think you are overthinking this a little bit.
I'm not 100% sure what that error is actually telling you in terms of your code, but it seems like you've probably got some wonky view returns because your setting the ViewBag information within a different controller than the one that returns the view. Am I understanding that part of it correctly?
Instead of creating another controller class, create a static function somewhere that handles the list creations and ONLY the list creations. When it creates a list, have it return it.
Then, whenever you need a list to be populated, do this in the relevant controller method:
ViewBag.DropDownDeviceType= *wherever-your-static-method-is*.PopulateDeviceType(model.DeviceTypeID);
I wanted use MVC and renderpartial to generate a menu but but could not get it to work, and from what I read it seemed maybe RenderAction would be more suitable. Still I have not gotten it to work.
What I intended to do was create a controller that selects certain articles from a database that will act as categories (this is put into HomeController):
public ActionResult MenuController()
{
var movies = from m in db.Art
where m.ArtikelNr.StartsWith("Webcat")
select m;
return View(movies);
}
And then send that information to a view:
#model IEnumerable<xxxx.Models.Art>
#{
Layout = null;
}
<ul>
#foreach (var item in Model)
{
<li>#Html.DisplayFor(modelItem => item.Benämning_10)</li>
}
This works when I just run it as a normal controller and view, it returns a list of what I want. But if I want to call it from _layout.cshtml (because this menu should appear on every page) like this:
<div id="sidebar">#Html.RenderAction(MenuController)</div>
Then it generates the following error:
CS0103: The name 'MenuController' does not exist in the current context
What is the proper way of calling an action/view/whatever from the _layout.cshtml file?
You should call
#Html.RenderAction("_MenuController")
and be sure that you have a working rule in your Global.asax
As suggested in another answer would be better to use
return PartialView();
I also suggest you to use the ChildActionOnlyAttribute to be sure that this action will never be called as a standard action.
So something like that:
[ChildActionOnly]
public PartialViewResult _MenuController()
{
var movies = from m in db.Art
where m.ArtikelNr.StartsWith("Webcat")
select m;
return PartialView(movies);
}
#{Html.RenderAction("MenuController");}
or
#Html.Action("MenuController")
Simply
#Html.RenderAction("MenuController")
You've forgotten quotes around your string parameter
<div id="sidebar">#Html.RenderAction("_MenuController")</div>
Quotes around your action name :) It might also be good practice to return a partial view:
return PartialView(movies);
I have following code
#using (Html.BeginForm())
{
#Html.DropDownListFor(m => m.IDs, new SelectList(Model. IDs), Model.SelectedID)
}
So user selection from this combo bind to SelectedID property of the model. My understanding is that this binding happen only when form is submitted. Let’s say from the same page, I need to do an AJAX call but at this point ) Model.SelectedID does not provide any value because form hasn’t been submitted yet (although user has selected something from drop down). Any ideas how to best deal with this situation?
You can use javascript.
var selectedValue = $("#IDs").val();
bind a change event to your DD
$("#DDL_ID").change(function(){
var currVal = $(this).val();
//do ajax
});
As others have pointed you would get this value with javascript on the change of the drop down list.
I wanted to point out however, that your understanding of the overload you are using for the drop down list is incorrect. This overload will display a default option box label.
For example you could prompt the users to select select something from the list:
#Html.DropDownListFor(m => m.IDs, new SelectList(Model. IDs), "Select Something...")
If you were to post the form in your example as is, you can see the selected item come across in the form. If your view model is setup in such a fashion the model binder would take over and bind this value to your "SelectedID" property.
[HttpPost]
public string DropDown(FormCollection form)
{
var selectedItem = form["IDs"];
return selectedItem;
}
I am still new to MVC, so sorry if this is an obvious question:
I have a page where the user can choose one of several items. When they select one, they are taken to another form to fill in their details.
What is the best way to transfer that value to the form page?
I don't want the ID of the item in the second (form) pages URL.
so it's /choose-your-item/ to /redemption/ where the user sees what was selected, and fills the form in. The item selected is displayed, and shown in a hidden form.
I guess one option is to store in a session before the redirect, but was wondering if there was another option.
I am using MVC3
Darin Dimitrov's answer would be best if you don't need to do any additional processing before displaying the /redemption/ page. If you do need to some additional processing, you're going to have to use the TempDataDictionary to pass data between actions. Values stored in the TempDataDictionary lasts for one request which allows for data to be passed between actions, as opposed to the values stored in the ViewDataDictionary which only can be passed from an action to a view. Here's an example below:
public ActionResult ChooseYourItem()
{
return View();
}
[HttpPost]
public ActionResult ChooseYourItem(string chosenItem)
{
TempData["ChosenItem"] = chosenItem;
// Do some other stuff if you need to
return RedirectToAction("Redemption");
}
public ActionResult Redemption()
{
var chosenItem = TempData["ChosenItem"];
return View(chosenItem);
}
If you don't want the selected value in the url you could use form POST. So instead of redirecting to the new page, you could POST to it:
#using (Html.BeginForm("SomeAction", "SomeController"))
{
#Html.DropDownListFor(...)
<input type="submit" value="Go to details form" />
}
To help others, this is how I resolved my issue of needing multiple buttons posting back, and wanting to pass a different Id each time.
I have a single form on the page, that posts back to my controller:
The Form:
#using (Html.BeginForm("ChooseYourItem", "Item", FormMethod.Post))
{
And the code
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult ChooseYourItem(string itemId)
{
TempData["ITEMID"] = itemId
return RedirectToAction("Create", "Redemption");
}
Then, inside the form, I create buttons whose name == "itemId", but has a different value each time.
For example
<strong>Item 1</strong>
<button value="123" name="itemid" id="btn1">Select</button>
<strong>Item 2</strong>
<button value="456" name="itemid" id="btn2">Select</button>