How to set a Default value for EditorFor [shape]?

Topics: General
Mar 26, 2012 at 11:32 PM

Hey guys, I have the following action:

        public ActionResult Register()
            // ensure users can register [I set this automatically in my migration]
            var registrationSettings = _orchardServices.WorkContext.CurrentSite.As<RegistrationSettingsPart>();
            if (!registrationSettings.UsersCanRegister)
                return HttpNotFound();

            ViewData["PasswordLength"] = MinPasswordLength;
            var shape = _shapeFactory.Membership_Register();
            return new ShapeResult(this, shape);  

Now I wish to contruct a default value for one of my viewmodel’s parameters so one of the views EditorFor's can have a default value, but was wondering how to go about it; as in, usually I would set the value in the constructor, then in the action: 

return View(new Viewmodel());

But being a shaperesult, how does one go about it? Thanks in anticipation, cheers Dyr

Mar 27, 2012 at 9:16 PM

You mean something like this?


var shape = _shapeFactory.Membership_Register( MyProperty: 42);
Mar 28, 2012 at 2:07 AM

Hey mate, thanks for the reply! Well *surpise, suprise*, I can't seem to get it to work? Here's my view 'Membership.Register':


@using TNS.Donations.ViewModels
@using Orchard.ContentManagement
@using Orchard.Core.Settings.Models

    //var signup = new SignupVM(); Would work, but breaks binding?
    var signup = (SignupVM)Model.Signup;

    HtmlHelper.ClientValidationEnabled = true;
    HtmlHelper.UnobtrusiveJavaScriptEnabled = true;

@using (Script.Foot())
<script type="text/javascript">

<h1>Membership Registration</h1> @using (Html.BeginFormAntiForgeryPost(Url.Action("Register", "Membership", new { area = "TNS.Donations"}), FormMethod.Post, new { id = "register-form"})) { @Html.ValidationSummary() <fieldset> <div class="wizard-step"> <h3>Step 1: Personal Details.</h3> <div> @Html.LabelFor(m => signup.UserName) @Html.EditorFor(m => signup.UserName) @Html.ValidationMessageFor(m => signup.UserName) </div> <div> @Html.LabelFor(m => signup.Email) @Html.EditorFor(m => signup.Email) @Html.ValidationMessageFor(m => signup.Email) </div> <div> @Html.LabelFor(m => signup.FirstName) @Html.EditorFor(m => signup.FirstName) @Html.ValidationMessageFor(m => signup.FirstName) </div>


                  @Html.LabelFor(m => signup.Country)
                  @Html.EditorFor(m => signup.Country)
                  @Html.ValidationMessageFor(m => signup.Country)




view model:


using System.ComponentModel.DataAnnotations;
using TNS.Donations.Models;

namespace TNS.Donations.ViewModels

    public class SignupVM
        private string _country = "Norge";

        [StringLength(50), Required, Display(Name = "Firstname")]
        public string FirstName { get; set; }

        [StringLength(50), Required, Display(Name = "Lastname")]
        public string LastName { get; set; }

        [StringLength(25), Required, Display(Name = "Username")]
        public string UserName { get; set; }
        [StringLength(255), Required, DataType(DataType.EmailAddress), Display(Name = "Email")]
        public string Email { get; set; }

        [StringLength(255), Required, DataType(DataType.Password), Display(Name = "Password")]
        public string Password { get; set; }

        [StringLength(255), Required, DataType(DataType.Password), System.Web.Mvc.Compare("Password"), Display(Name = "Confirm password")]
        public string ConfirmPassword { get; set; }

        [StringLength(50), Required, Display(Name = "Street")]
        public string Street1 { get; set; }
        public string Street2 { get; set; }

        [StringLength(25), Required, Display(Name = "City")]
        public string City { get; set; }
        public state State { get; set; }
        public short PostCode { get; set; }

        public string Country {
            get { return _country; }
            set { _country = value; }  

so, therefore - amending the models Country get/set's and removing '_country' and just having:

var shape = _shapeFactory.Membership_Register( Country: "Norge");

in my action *should work*, but doesnt, I assume due to assigning the model to 'SignupVM' in the view? Thanks for the support, cheers Dyr

Mar 28, 2012 at 8:25 PM
Edited Mar 28, 2012 at 8:26 PM

Oh, but where then do you pass the "signup" view model to the view? Based on your view, I would expect a call like this from your action:

var signup = new SignupVM { Country = "Norge" };
var shape = _shapeFactory.Membership_Register( Signup: signup );
return new ShapeResult(this, shape); 

So simply create and setup your view model from your action, then pass that view model into the shape. The shape template (your view), as it is now, should then work. Although I never garuantee such things ;)

Mar 30, 2012 at 1:49 AM

Your surmising is as good as a guarantee in my book; ‘once again, spot on!’ Cheers mate