Within a view I have a button which navigates to another view 'addWork'.
I want to pass an observable from the view model to the view model associated with the addWork view.
I already have addWork within the constructor for the view model, I'm just unsure what else to do.
The url action on the ko.applybinding is as follows, this work fine, I just need to amend so I'm passing the observable.
"#Url.Action("AddWork", "Work")")
The button is bound to this function:
self.AddWork = function () {
window.location.href = addWork;
}
An observable is purely a javascript concept and typically won't survive being sent to the server and back. You would be better off sending just the contents of the observable instead of the observable itself.
If you just need to send an ID then with asp.net MVC you can put it into your #Url.Action statement like Url.Action("AddWork", "Work", new { id = myID });
Related
Is it possible to cast a model going into a partial view?
#Html.Partial(Model.Partial, new { model =
((WarningPopupModel)CommonData.NotificationPopup.PopupModel) })
Here PopupModel is of type object but holds an instance of WarningPopupModel, when I try this is the error I get
Additional information: The model item passed into the dictionary is of type
'<>f__AnonymousType6`1[EmployeeKiosk.Models.WarningPopupModel]',
but this dictionary requires a model item of type
'EmployeeKiosk.Models.WarningPopupModel'.
So really I need to understand the 'f__AnonymousType6' part and know what kind of flexibility I have here
Background.
I want to create a popup in the view depending on some business logic, so ultimately the controller will pass back the name of the view (or it could be a token) together with some model.
In the view I just need some way of being able to switch between the partial views that appear, the partial views will be Kendo popups
thanks
Pretty sure that you should just be able to change it to;
#Html.Partial(Model.Partial, (WarningPopupModel)CommonData.NotificationPopup.PopupModel);
Without the new { model = ... } part.
I am developing MVC application.
I am in situation that, I want to call the method of controller from View.
I don't want to use the Submit button (If I use it goes to create method.) either I dont want to use ajax ....
I want to know that what are ways to call the method of controller from view...
Other than submitting form process or other than ajax call.
Ex. I have a form in which user fills all the data and save it... After submit button it saves the data in DB and Saved data displayed. Now, After saving I want to transfer/pass that data from that view to another view... but I cant find appropriate method for it.
Please check image below...
I have this view after saving...now when I click on 'Click Here' Link , I want to pass the same data to another view to show the preview... some other format...
So I am stuck on how to pass the same data ? I cant use submit as it already saved... I cant use ajax as its not partial stuff... I want to pass the entire to to another view...
If you have the order saved in a database on the server, you would typically pass through the ID, rather than the "entire data" to the controller method, and reload the data from the DB in order to render the other view. Is this what you're after?
View code:
...sent successfully. #Html.ActionLink("Click Here", "Status", new { id = Model.Id }) to check status
Controller Code:
public ActionResult Status(int id)
{
var order = Repository.Get<Order>(id);
return View(order);
}
I have a controller with a method which returns a partial view and generates ViewData. Then, I have some methods, each of them returns Json objects and ViewData. But, the ViewData is not getting refreshed. How to refresh it? Is this possible?
public ActionResult FirstMethod() {
ViewData["someList"] = ...;
return PartialView(someOtherList);
}
public JsonResult SomeMethod()
{
ViewData["someNewList"] = ...;
return new JsonResult { ... };
}
But, although SomeMethod() is called after FirstMethod(), the ViewData which I use in my view is someList.
Is this possible?
No, it's not possible. The ViewData is a weakly typed dictionary which could be used (although I wouldn't recommend using it) to pass information between a controller and a view. Its lifetime is tied to that of the controller action execution pipeline. So once the view is rendered it's over. No controller, view, ViewData, ... exists anymore. Only HTML rendered in the client browser.
Then you send an AJAX request to the server again. This is completely new request that has nothing to do with the first one (which was used to render the view initially) and thus a new instance of the controller is created with its own ViewData. Setting ViewData in a controller action that returns JSON is useless because since this action will be invoked with javascript, all the information that you want to pass from the controller to the javascript success handler must be part of the JSON object that you return.
I inherited an MVC app and am a true novice at it coming from an ASP.NET environment. My problem is I can't manage to move variable data between my partial views and controllers. In ASP.NET this was pretty straight forward, not the case in MVC. So the following code fires when a Tab is selected from the client but I need to capture the selectedTabIndex value from the client and pass it to the controller.
function onTabSelect(e) {
var selectedTabIndex = 0;
selectedTabIndex = $(e.item).index();
alert(selectedTabIndex);
}
I considered using the ViewData object but there doesn't appear to be a way to make perform this task of assignment from within the function. So the following code would seem to be a ridiculous task and it fails anyway. (Remember, I am an extreme novice at this)
function onTabSelect(e) {
var selectedTabIndex = 0;
<% =ViewData["selectedTabIndex"]%> = selectedTabIndex;
}
I also considered using cookies but that doesn't seem to be a practical method either. In ASP.NET I could access the client controls using the .find method but this is a steep learning curve for me.
What are my alternatives?
If different tabs can be selected by the user between Save operations, you might need to consider binding some kind of click function (using jQuery) to set the selectedTabIndex javascript variable. Alternatively, you could set a hidden input's value to the selected tab index.
In either case, if you need the value in your controller (to set ViewData, ViewBag, some model data, etc) when a Save operation is submitted, you could submit the data via $.ajax and return some JSON from your controller
$('#SaveButton').click(function() {
$.ajax({
url: '/Controller/Save',
type: 'POST',
// you might need to serialize additional data you want to save here
// but you could create any JSON object before calling $.ajax
data: {"selectedTabIndex": selectedTabIndex}, // data to POST
dataType: 'json', // this indicates the type of data returned from controller
success: function(data) {
// you didn't really describe how to programatically set a tab,
// so "setToTab" is a guess
$('#tabID').setToTab(data.selectedTabIndex);
}
});
}
And your controller might look something like
public ActionResult Save(int selectedTabIndex)
{
// again, you might need different parameters to complete your Save
return JsonResult(new { selectedTabIndex: selectedTabIndex });
}
Data is sent to a controller via a HTTP POST. Create a hidden input <input type="hidden" name="selectedTabIndex"/> and set the value with javascript prior to POST. The controller can extract this value from the Form parameters, or it can be autopopulated in the Action method if the method contains a parameter matching the input name.
This is a non-ajax alternative to the answer provided by David.
So like the title says, I created a view model in my asp.net mvc application so it would be strongly typed to my view. My view model is a combination of two of my model classes. Now when the user hits the save button on that view, it goes to a controller. How does it know what controller to go to? I built my controller 1 - 1 so to speak with my models and views so controller A knows about Model A and controller B knows about Model B. But if I have a view model that is AB how does it know on subit to go to A or B. Do I need a controller called AB Controller?
The controller and action invoked do not depend on the viewmodel object you used to bind the page during initial view rendering. The controller (and action) invoked is determined by the URL of the request sent. (One of the routes you have defined will be matched based on the URL string of the request or a 404 not found error will be returned.)
A submit button in an HTML form (usually a POST) will have an action attribute that determines its url target, an anchor tag will have an href, etc.
In your situation where you have a custom viewmodel object you can define an action to expect and to attempt to parse that specific type of object by specifying it as the parameter to your action:
public ActionResult SaveSystemSetting(SystemAdminVM item) {
An Action Method doesn't receive a model. It receives parameters.
Anyway I think I know where you come from: One of the parameters of your action has the type of the ViewModel used in the View. That is a common layout and makes a lot of sense. In lots of projects it is so widely used that after some time you start to think that an action actually receives a model.
Just remenber that the parameters of your action can by anything that can by filled by the ModelBinder. You could pass both Models as parameters, you could pass a ViewModel that agregates Model a and b or you could pass a ViewModel that has properties of a + b.
Agregation ist the most common approach.
Generally we overload our action methods in the controller so if you have an action called edit that renders the view with your viewmodel object, you will have an overloaded edit action method with the HttpPost specified for that method.
The data to this method will be passed as form value collection values or you can bind it to your viewmodel object if you like and process it further.