I try to use one partial view for all classes implementing particular interface
#{Html.RenderPartial("_PaymentItem", Model.RentItem);}
#{Html.RenderPartial("_PaymentItem", Model.EquipmentItem);}
#{Html.RenderPartial("_PaymentItem", Model.InterestItem);}
#{Html.RenderPartial("_PaymentItem", Model.ForfeitItem);}
#{Html.RenderPartial("_PaymentItem", Model.SupplyItem);}
All PaymentItems are part of the model. After I post the page all payment items bind the values from the last rendered partial view. I assume the problem is that the partial view generates same ids and names for each invoke. How can i bypass this ?
Thanks in advance, I'll appreciate any help.
Have you tried casting your model properties to the particular interface?
EDIT:
What I mean is to specify the model
e.g.
#model IViewM
on the first line of your _PaymentItem file.
Related
I have many screens that have different business logic, but they are looking similar. I want to create one screen for ios (storyboard or xib), and one MvxViewController, but many different (shared) view models.
I cant find a correct way to do it.
One way to achieve it is to have a base view with a generic parameter that will be your viewmodel and make as many views as you need inheriting from that one and on each of these views you set the corresponding viewmodel on the generic parameter.
public abstract class MyBaseViewController<TViewModel> : MvxViewController<TViewModel>
{
// My view init and logic
}
public class MyView1 : MyBaseViewController<MyViewModel1> {}
public class MyView2 : MyBaseViewController<MyViewModel2> {}
And so on.
Another way a bit more difficult would be to modify your presenter to create a new view with a different viewmodel every time you display a new ViewModel
HIH
I'm currently working on a homepage where I am showing the 'latest hauls' and 'latest finds' they are both separate models ofcourse.
Currently I am only showing the 5 latest hauls since I use the HaulController#getWelcome Controller so I can access $haul->title etc.
How would I be able to also access $finds->title?
Thanks for the help!
You can share a model to a view by using it inside controller function -
View::share('subadmin', App\Models\SubAdmin::class);
You can call the model in your view like
{{ \App\Model::function() }}
So you can have {{ App\Model::orderBy('created_at', 'desc')->limit(5)->get() }}
You have a couple of options.
Option 1: A combined model and one view.
To do this create a new ViewModel and have LatestHauls and LatestFinds as properties on this model. You could set these properties in the controller.
On your view simply navigate down the viewmodel to the appropriate properties.
Option 2: Extend the above solution to use partial views. Create a partial view for each model. On the parent view call each partial view. This might be considered a better solution but option 1 will get you started.
In Core MVC if you want the razor view engine to look for views in places other than the default locations then a new ViewLocationExpander can be added in the ConfigureServices method in the startup.cs like so
services.Configure<RazorViewEngineOptions>(options => {
options.ViewLocationExpanders.Add(new ViewLocationExpander());
});
Where the ViewLocationExpander class implements IViewLocationExpander. That class typically has access to the ViewLocationExpanderContext, and in RC1 that context contained an IsPartial property. This property was useful if the location expander wanted to specify different view locations for partial views than regular views.
I see that in RC2 this IsPartial property is gone. However, I do see that there is a IsMainPage property but I can't find any documentation on it.
Does anyone know if the property basically just the inverse of the old IsPartial property?
Yes, the IsMainPage property is as you've said - the inverse of IsPartial. As Pranav has pointed out in the comments, you can see the commit and reasoning for the change here.
Glad to see I'm not the only one that was caught out by this change!
I am not sure if this is the best approach but I have a controller that originally I intended to control a show index that renders many partials on it (a header partial and then, has some if else magic to render different partials based on the step the user is in in filling out a form... a form has many sections across several pages). I think ultimately ajax is the way to go but I am not even to that point yet. I am not sure this is the right way to do it, so I guess that is what I am asking... is the many different partials to one controller the way ? or does each "page" of form data have to be broken out into its own controller? allowing the user to fill out form (check boxes, comment section) and click "next" passing the model of the data they are filling out along the way and saving that model in each next?
U may not need several controllers, but 1 controller with some actions may be a good start. =)
Then each action should load only the partial it needs. like u can give the action name to the partial, making easy to know which partial to render.
Or maybe u can try to use wicked.
There is a railscasts for it.
Well, you could use a method to decide which partial to render.
Use this example or do some meta programming.
class YourController < ApplicationController
def index
render :partial => partial_selector(param)
end
private
def partial_selector param
#logic to decide what partial do render
#returns the partial name
end
end
I have a C#.Net web app and I am trying to access one of the HTML/ASP Text Boxes in the Controller for the Edit View of my Proposal model. In a non-MVC app, I was able to do this using Control.ControlCollection.Find(). Is there an equivalent for a MVC3 project?
You ask for an equivalent of Control.ControlCollection.Find() in MVC?
In MVC your controller is not aware of controls.
The controller just receives data via parameters and returns data via the function result.
What do you want to do with the control in your controller code?
If you want to access the value, you should bind it to a parameter:
View:
<input name="MyControl" type="text" />
Controller:
public ActionResult MyAction(string MyControl) {
// MyControl contains the value of the input with name MyControl
}
The MVC pattern was designed to keep things separated.
The View has no knowledge of the controller at all
The Controller only knows that a view exists and what kind of data that it needs. It do not know how the data is render.
Hence, you can never get information about controls/tags in the view from the controller. You need to use javascript/jQuery in the view and invoke the proper action in the controller.
In an MVC-application you don't have controls like in a webform-application.
In MVC you collect your required data in the controller and pass it to the view.
Typicaly the view is a HTML-page with embedded code.
In opposite to controls in webforms which produce HTML and handles the post-backs in MVC you have to do all this manually. So you don't have controls with properties and events wich you can access easily in the controller and you have to handle all your posts with your own code.
Thats sounds as it is a lot of more work - and indeed it could be if you implement the behaviour of complex controls - but MVC applications are much better to maintain and you have 100% influence to the produced HTML.
Well probably i am late for this but it should help others in future...u can store ur value in hidden field in view and then access that value in controller by following code..
Request.Form["hfAnswerOrder"].ToString();
Point - hfAnswerOrder is the ID of the hidden field
My Control in cshtml page..
#Html.Hidden("hfAnswerOrder", Model.Answers.ToList()[0].AnswerOrder)