I am creating a web portal. I have a button on the portal which redirects the user to a different website (owned by us but completely different website). I would like to pre-fill some of the input fields on a form with data passed from the portal (First name, last name and email).
I understand you can use query strings in the URL to populate fields on a form. I have tried using mywebsite.com/page/?FirstName=Jane but it is not populating the input fields. This method seems ideal but I assume there may be some work to do on the separate website to get this to work.
Both my portal and the other website is built with asp.net mvc.
Can anyone confirm if can get away with passing parameters without modifying the different website?
It's been a while since I've done mvc.
if you want to link with mywebsite.com/page/?FirstName=Jane
Assuming you have control over the page you're linking to, the page that you link to needs to
1. The model needs to have firstName in it
public class SomeModel
{
[Required]
[Display(Name = "First name")]
public string FirstName { get; set; }
}
The controller needs to populate it from that query string
public ActionResult Index(SomeModel model)
{
return View(model);
}
The view needs to use it
#model Site.Models.SomeModel
#Html.TextBoxFor(m => m.FirstName, new { #class = "form-control" })
I'm using Orchard's 'Custom Forms' module to build a form by adding a bunch of fields to a content type called 'Booking Form'. It works just fine - I can submit the form and view submissions in the admin pages.
I want to use client-side validation on the fields. I've succeeded in getting jQuery unobtrusive validation to work on my own manually-built forms (using #Html.EditorFor() a property on a viewmodel, and decorating the field [Required]). However, I can't get it to work in this context.
I am guessing this is because the property that's being used to render the input is not decorated with DataAnnotations attributes:
namespace Orchard.Core.Common.ViewModels {
public class TextFieldDriverViewModel {
public TextField Field { get; set; }
public string Text { get; set; }
public TextFieldSettings Settings { get; set; }
}
}
I think the 'Text' field would need something like [Required] before it for unobtrusive validation to work properly.
Is there any way to achieve unobtrusive validation for fields like this? Obviously decorating the property is undesirable, as then even fields NOT marked as required in the CMS would become required.
I'm trying to learn MVVM and as a new person without a tutor, sometimes it gets sooo confusing. right now I have this problem:
** in one window, I have used 3 different views from different modelviews.
** each view is for selecting one item from a branch and we will traverse the tree.
** in first View, I choose my book
** the 2nd view will show pages of that book
** the last view I want it to show exercises of that page.
so far I was able to bind the 1st view to the Books. now, how should I bind the second view's source to the 1st one's slectedItem ?
MVVM Problem http://clickasun.ir/8283kitchen/images/mvvmproblem.jpg
there are several ways to achieve what you want. eg.
public class Exercise{}
public class Page
{
public List<Exercise> MyExercise {get;set;}
}
public class Book
{
public List<Page > MyPages {get;set;}
}
viewmodel
public ObservableCollection<Book> MyBooks {get;set;}
xaml
<ListBox x:Name=books ItemsSource="{Binding MyBooks}"/>
<ListBox x:Name=pages ItemsSource="{Binding ElementName=books,Path=SelectedItem}"/>
<ListBox ItemsSource="{Binding ElementName=pages,Path=SelectedItem}"/>
this is of course just one way to do it.
ps: code handwritten, so check for errors
OK I want to thank anyone who read this and tried to help, Specially yo guys "Blindmeis, Jen H"
thank you.
I know this scenario had to be so general, I just couldnt find it.
so I found 2 approaches to it that both can be good solutions.
Use Microsoft Prism. (Its a pattern to make modular programs in WPF with ease)
MSDN LINK
Another LINK
Use Master Detail Pattern
Here it is clear
So I got mine solved. Hope it would be useful for you too.
I have come from WPF (MVVM) background and trying to shift to MVC 2. Is there any pattern in MVC2 where you can use Commanding/Command buttons like <input> which you use to submit the form so that you can hide/disable when you try to Render the View.
In MVVM world, your commands could implement ICommand interface, and it had CanExecute method which was quite useful. I was wondering if there is anything similar in ASP MVC 2 ?
The only way I can think of, is to do it in the View, so that I can check the flag on ViewModel (CanSave) and depending on that show/hide the <input> tag.
Basically I want to have 2 version of the website running, one in Read-Only mode and the other Editing mode.
Let me know if you need any clarification.
ASP.NET MVC does not feature the notion of 'controls', as are found in classic ASP.NET and WPF. The foundational blocks of ASP.NET MVC are HTML elements, like <input>, <button> et cetera. Naturally, these don't offer the functionality you're looking for (i.e. Implementation of the ICommand Interface).
The scenario that you're looking at (i.e. two modes of your form) can be (and arguably should be) dealt with at the View level. You're already facing the right direction: have a 'CanSave' property on your Model, and use this in the View to determine what is generated.
Example:
<% if (Model.CanSave)
{ %>
<p>First Name: <%= Html.TextBox("firstname", Model.firstname) %> </p>
<% }
else
{ %>
<p>First Name: <%=Model.firstname %></p>
<% } %>
You'll probably want to check out the DisplayTemplates and EditorTemplates... very handy for this scenario. Brad Wilson does a good job here.
It will help you move to this:
<%= (Model.CanSave) ? Html.EditorFor(x => x.firstname) : Html.DisplayFor(x => x.firstname) %>
...which makes your View clean and nice.
If you can't get MVC to do this it's relatively worth it to hand-code something like this vb-style pseudocode. This involves...
Subclassing your controls.
Not as much of a pain as it sounds, but, it is a medium sized one. Therefore it is only appropriate for medium-sized to large apps. But worth it for them.
Interface BaseUIControl
Property Enabled as Boolean
Property Visible as Boolean
Property Name as String
Property EntireStateAsXML as string ' You can use this to do EVERYTHING!
Interface UserActionItem
Event Clicked(sender as UserActionItem ... don't pass anything from UI namespaces!)
Class MyButton (or link, etc.) Implement BaseUIControl, UserActionItem Inherits UI.Button
How does this help? You've basically replaced the missing functionality. Your Controller (or even application layer) can be aware of the UI components by interface only, so they won't have to see the UI types.
more...
You can leverage this philosophy to control everything. This has saved me thousands of hours of monkey code.
Interface TextControl
Property Value as text
Interface CheckControl
Property Checked as boolean
The above two are Pretty basic - you inherit MyCheckBox and MyTextBox from the UI versions and implement the appropriate.
Of course you could set up common code to loop thru all controls and auto-validate (or loop thru and get each one's XML to autobind the whole form).
Interface ValidationBase
Property Required as Boolean
If you have a text or numeric-only mask or restricitons built into 2 subclasses...
Interface ValidationNumeric
Property MinVal, MaxVal as double
Interface ValidationText
Property MinLen, MaxLen as double
No, it won't go to the database for you. But this sweeps a ton of crud under the rug.
You can even set these property values in the UI designer - yes, putting BL in bed with UI, BUT, if you only have one UI for the BL, actually works very well.
Now image a UI with a mix of things like listbox/multiselect, double-list picker controls, checked listbox, a groupbox of option buttons/checkboxes ...
Interface Selector
property Items as list (of string)
property SelectedItems as list (of string)
Use what works on the UI - your generic routines can care less what they look like!! The subclassed UI pieces will just implement them to set/get the right values.
In addition ... we added 'validationEquation', ActivatesEquation (gray/ungray), SetValueTriggerEquation (if true, set value to SetValueEquation, otherwise, leave alone), which allowed controls to be set to simple values from other items (basically getting the values from bound objects as if using reflection) via Pascal Gayane's Expression Evaluator (it reads .net types!)
You can also subclass the main form, have it recurse thru all it's subcontrols, put together the XML's for the whole screen, and serialize it like that. You can have your own classes implement these in the non-UI layers and use it to totally (de/)serialize the UI state, and use them to read the UI too, if they relate to a business object, to map to it.
It's unbelievable how much this simplifies a complex app. We have one with 1200+ data entry panels (... pages... ours is a thickclient app) that will fill out 250 different paper forms at 250K LOC. The form definitions contain the 'name' of each control and this is pulled from the XML generated from the screens. We probably saved 500K LOC as many of the screens have no code behind them or only trivial code; all the databinding, validation, etc. is handled by common routines that reference the interfaces.
Like I say, this only works for a big app. Spend at least 2-3 weeks developing 90% of the functionality, though; probably another month throughout the 2 years dev maturing it. I am guessing your apps is big if you're caring about ICommand and its conveniences. I would put the payback at 15-20 moderately complex pages.
If I'm understanding the question correctly, you could write a ControllerCommand class to encapsulate this. Something like this:
public class ControllerCommand
{
public string Action { get; set; }
public string Controller { get; set; }
public object RouteValues { get; set; }
public bool IsEnabled { get; set; }
}
Your Details viewmodel might use it like this:
public class DetailsModel
{
public guid Id { get; set;}
// some other viewmodel properties
public ControllerCommand Edit { get; set; }
}
You could write extension methods on HtmlHelper to replace the built-in ones:
public MvcHtmlString CommandLink(this HtmlHelper html, string linkText, ControllerCommand command, object htmlAttributes)
{
if (command.IsEnabled)
{
return html.ActionLink(linkText, command.Action, command.Controller, command.RouteValues, htmlAttributes);
}
else
{
return MvcHtmlString.Create(linkText);
// perhaps return <span class="disabled-command">linkText</span>
}
}
One of the ways I have found is to use Filter attributes which you can put in your Actions, but that only handles CanExecute on the server side.
For the GUI side, couldnt find better way than putting If statements to check if the user is Priviliged to run particular action (i.e. Edit/Delete buttons)
Let's say I have a Workflow with 2 dependency Property : Prop1, Prop2.
I'd like to create a custom activity that when I drag into the workflow, It will show Prop1 and Prop2 in the property grid in the designer.
Is this possible ?
Like the invokeWorkflow, when you select the TargetWorkflow, it populates the property grid with Parameters of the workflow, so that you can bind.
You could try something like this:
http://blogs.microsoft.co.il/blogs/bursteg/archive/2006/10/29/DynamicWorkflowBindingParameters.aspx
I've been doing quite a bit of digging into dynamically creating properties during design time and I've had some success with it.
However, I haven't been able to get dynamic properties to show up in the actual property binding display. So you can create properties dynamically in the designer and set them, but you can set other properties to point to your dynamic properties.
This appears to be a limitation of the workflow designer in visual studio. I can't see a reason why the workflow engine itself can't handle this.
You shouldn't need to do anything, by default all public properties are displayed in the property grid.
If you define each one of your properties like this, the binding should be available:
[DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
[BrowsableAttribute(true)]
[CategoryAttribute("Parameters")]
public static readonly DependencyProperty CustomParamProperty
= DependencyProperty.Register("CustomParam", typeof(int), typeof(CustomActivityClass));
public int CustomParam
{
get { return (int)GetValue(CustomParamProperty); }
set {SetValue(CustomParamProperty, value); }
}
Good Luck!