Accessing Placeholder From Widget MVC Controller Code - sitefinity

I'm using Sitefinity 8.1 in MVC mode. I have an MVC page template with an assortment of placeholders.
I've created an MVC widget designed to be used multiple times on a page. From within the controller code I would like to be able to find out the name of the placeholder the widget is sitting in so that I may make adjustments to the widget on-the-fly.
Is this possible?
Thank you.

Instead of relying on the name of a placeholder, which I think is prone to errors, why not just introduce a public property in the controller?
e.g.
public string Message {get; set;}
Then when you drop the widget in placeholder1 you can edit its properties and put whatever you want in the Message property.
Similarly, when you drop the widget in placeholder2 - you edit its properties and set something else in the Message property.
The controller will do different things depending on the value of the property - this way it is much cleaner than relying on placeholder name.

Do the adjustments need to be server side? If not just make changes based on CSS selectors. If server side I'll have to get back to you.

Related

DNN get params in view are not available in edit

I am creating a dnn module. The content depends on the param in the url.
I want to be able to edit this content in the 'edit content' mode. However when i go to edit content the param in the url is no longer accessible because it is the parent document. How do i go about passing this value from the view.ascx to the edit.ascx?
Try storing the param in cookies or localstorage. Then you should be able to access it. Of course the user will be able to modify it but you can do a check that the user has not modified it by storing a server side encryption or somthing like that.
A workaround is to have a field where the user enters this parameter. But i know this isn't a very good solution. I am guessing that you will have to override the dotnetnuke core to do this (yes i know it sucks).
I hope I am understanding the question properly.
To pass parameters from your View to Edit controls, you should first make sure they are registered properly in the module definition. You default View should have an empty controlkey and your Edit should be registered with a control key, for example "addedit".
When creating a link between your view control and edit control, use the EditUrl() method of PortalModuleBase. When passing a parameter, for example an id of the item you want to load into your edit control, you can pass them as arguments in the EditUr method.
Example (in my view.ascx.cs):
lnkEdit.NavigateUrl = EditUrl("id", "16", "addedit");
This will assign a module view link to the edit.ascx (assuming the controlkey in the definition is addedit) passing in a url parameter "id" with value 16.
See my DNN Module Views tutorial for a complete lesson on how to do DNN module views and navigation.
http://www.dnnhero.com/Premium/Tutorial/tabid/259/ArticleID/204/Introduction-and-Module-Definition-basics-in-DNN-Part-1-6.aspx

DNN 7 Custom Profile Property with database validation

I'm using DotNetNuke 7 for my CMS. I created a custom profile property for zip codes. Does anyone know how to add custom validation for the data entry before allowing the user to register.
Thanks
I don't think DNN allows this in custom properties, you'll likely need to wire something up yourself to make this happen. You might be able to add some custom jquery to the registration page that does some webservice calls to check and validate a property, but I haven't tried that before so it would probably be a lot of trial and error until you get it working right.
Click on the pencil next to the property you created if its only one zip code then in the valid expression area put the zipcode ex:08109 of you wanted to have it at two zipcodes lets say 08109 and 08110 then you could use a validation expression something like this:
^[0]{1}[8]{1}[1]{1}[0-1]{1}[0-9]{1}$

Accessing controls in Razor view from controller MVC4

I am new to MVC. In my application the view page created using razor by designing it from another html page designer. I have doubt that how to access the html controls from the corresponding controller. For example, i create a controller named Home and and corresponding view. Added a text box into it.
<input id="name" type="text" name="txtName"/>
Now i want to get and set the value in text box from controller without using script.
Just like
txtName.text="...."
Is that possible..?
It sounds like you are thinking of things from a Web Forms perspective, e.g. controls, setting properties server-side.
MVC allows much better separation of concerns, that is, each piece should play its part without being tightly coupled to other parts. Having a controller set a property of a textbox in a view means that there must always be a textbox in that view and would tightly couple the controller to that particular view. It is not directly possible and it would be a bad idea even if it was.
That's where view models come in:
// M - model
public sealed class MyViewModel
{
public string Name { get; set; }
}
// V - view
#model MyViewModel
// (usually code to begin a form goes here)
#Html.TextBoxFor( o => o.Name )
// C - controller
public ActionResult MyActionMethod()
{
var model = new MyViewModel { Name = "Hello" };
return View( model );
}
It may seem like an extra step (it is) but it is cleaner, far more flexible, and far more test-friendly.
It would only be possible with another request to the server (e.g. POST, GET) because the Controller code can only run server-side. After processing another request, you could use a ViewModel to populate your HTML text-box while rendering it, but I doubt that is what you are looking for.
If you are familiar with desktop programming (e.g. Window's Forms) and you are looking to immediately change and process fields on an HTML page, you will need to use JavaScript. If you are unfamiliar with web-programming, or even just new to the MVC paradigm, I suggest you try out a few MSDN tutorials.

ASP.NET MVC Set texts in the View or in the Controller

What is the best practice in MVC (for testing, SOC and scaffolding) for setting texts (for exemple page title, h1,h2..)
Is it better to do it in the controlle, fill a viewmodel and send it to the view
or directly typing texts in the view?
Also I will propably use ressouces files for global texts (like button text, menu texts) and local ressouces for view specific texts.
The view is merely to present the output to the user. Depending on your requirement you can either:
1) Type the text in the view directly <h2>Hello World</h2> (for static content that will not change)
The latter two options are for ideal for dynamic content, where the content could be received from a database or some additional input.
2) Use the ViewBag to pass information to the view (in which case you would set it in the controller ie. ViewBag.HelloWorld = "Hello World" : <h2>#ViewBag.HelloWorld</h2>
3) Use a model to pass the information to your view. This should be your preferred option where possible. In your specific case you could use the controller to retrieve the content from your global resources, bind it to the model and pass it to the view.
The logic on where to get the data should come from the controller and the View's function should be to merely display it.
I recommend binding properties on the viewModel to the UI.
This is not only more testable, but it's also more flexible. It makes it easier to implement features like mult language support etc.
I recommend avoiding hard coding text in the markup if you can

Vaadin 7 - Set and get attributes into VaadinSession

I'm currently using Vaadin 7 for creating a RIA and I'm designing the Login functionality.
The application design is simple:
A UI class performs navigation beetwen different View classes. In particular, the first displayed View is a LoginView class and after a user has been authenticated the UI class redirects the user towards a MainView.
After authentication I'd like to set User data (eg name and surname) into the session and display it always (no matter what specific view) on the top right corner of the Web Application.
As a begginner I read the Vaadin Book and I firstly used the scheme illustrated in chapter 11th https://vaadin.com/book/-/page/advanced.global.html, but it doesn't seem to work as expected, probably because I'm using View navigation scheme.
So I use the following approach:
Once authentication is correctly completed LoginView tries to store user data as a VaadinSession attribute with the following code:
VaadinSession.getCurrent().setAttribute("name", name);
then UI navigates to MainView and tries to get the name of the user with:
VaadinSession.getCurrent().getAttribute("name");
but it gets a null value.
Does anyone know why? I appreciate any help.
Thank you.
I would suggest storing the values on the HTTP session instead.
VaadinServletService.getCurrentServletRequest().getSession().setAttribute("name", name);
and
VaadinServletService.getCurrentServletRequest().getSession().getAttribute("name");
Use the #PreserveOnRefresh annotation on main class which is extends the UI.
Then you can store variables data in the main class.
You can give it from anywhere (in any View) with ((MyUIClass)UI.getCurrent()).getSomeVariable method expression.
I have a guess as to why you get your null value. In most tutorials of modern Vaadin, the navigation pages are created first-thing in the UI as objects. If one of these views instantiated in this process is the MainView. If the label displaying the name sets the String in the constructor of the class, the line:
label.setValue(VaadinSession.getCurrent().setAttribute("name", name)); will be null is the attribute has not been set by the login function yet.
You need to either update the label showing the user string in the #override enter() function, or handle it otherwise.