Model data is not reflecting the changes in the form

Topics: Customizing Orchard, General
Feb 21, 2012 at 7:47 AM

I am having problem with the Model data in my custom module. I am developing a module which will retrieve the data from external system and allow users to update the data in a Orchard form. Retrieving and showing the data is not a problem but when i try to change some values on the form and update the form, the model is not reflecting any data and it is null in the HTTP post. I tried to test by creating a very simple form as below and the result also is the same. Is there anything wrong with my code or did i miss something?

I have created a content part and content part record as follow:

 

 [OrchardFeature("CRMProfile")]
    public class CRMProfileRecord : ContentPartRecord
    {        
       
        public virtual string UserName { get; set; }
      
        public virtual string FullName { get; set; }
      
        public virtual Guid MemberId { get; set; }
    }

 

 

 [OrchardFeature("CRMProfile")]
    public class CRMProfilePart : ContentPart<CRMProfileRecord>
    {
      
        public string UserName { get { return Record.UserName; } set { Record.UserName = value; } }
       
        public string FullName { get { return Record.FullName; } set { Record.FullName = value; } }
       
        public Guid MemberId { get { return Record.MemberId; } set { Record.MemberId = value; } }
    }

 

This is my migration file :

 

 public class Migrations : DataMigrationImpl {

        public int Create() {
			// Creating table Ibiz_Crm_LoyaltyPortalModule_CRMProfileRecord
			SchemaBuilder.CreateTable("CRMProfileRecord", table => table
				.ContentPartRecord()
				
				.Column("UserName", DbType.String)
				.Column("FullName", DbType.String)
				.Column("MemberId", DbType.Guid)
			);



            return 1;
        }
    }

 

This is the controller code:

 

public ActionResult Test()
        {
            CRMProfileRecord record = new CRMProfileRecord();
            record.FullName = "test";
            return View(record);
        }

        [HttpPost, ActionName("Test")]
        public string TestPost(CRMProfileRecord record)
        {
            if (string.IsNullOrEmpty(record.FullName))
                return "fail";
            else return "ok";
        }

 

This is my test view.cshtml. Here I tried using both normal input control and also the HTML.Textbox helper.

@model Ibiz.Crm.LoyaltyPortalModule.Models.CRMProfileRecord

@{
    ViewBag.Title = "Test";
}

<h2>Test</h2>
@using (Html.BeginFormAntiForgeryPost(Url.Action("Test", "Admin", new { Area = "Ibiz.Crm.LoyaltyPortalModule" }),
        FormMethod.Post, new { enctype = "multipart/form-data" }))
{ 
 <label for="test" style="font-weight: bold">@T(Model.FullName)</label>
                    @*Html.TextBox("Model.FullName", Model.FullName)*@
    <input type="text" id="Model.FullName" value=@Model.FullName />

    <button class="primaryAction" type="submit">@T("Save")</button>
}

and the page is always showing fail.

Regards,

Thor

Coordinator
Feb 21, 2012 at 7:53 AM

Why do you need multipart?

Feb 21, 2012 at 8:01 AM

I am just trying to follow the sample in http://docs.orchardproject.net/Documentation/Understanding-data-access

I need to store the user's guid key in database to retrieve the data from external system and I thought it content part is the way to store the data in orchard database. And in my actual module, i am pulling some other data and show it in the form using another class in view model. The problem is whenever i click on the save button, my modified data is not reflecting in the post function.

Feb 21, 2012 at 8:35 AM

Even i remove the multipart, it is still not working.

Coordinator
Feb 22, 2012 at 1:14 AM

You mean that data is not related to a content item? If this is not a content item, don't use a part, use a plain record and do your data access through IRepository<YourRecordType>.

Feb 22, 2012 at 2:42 AM

I tried that way before I created the part. I can access and display the data on the form but when the form was submitted through post, as I said before, the data model is with 0 records and it is always showing as fail as in my example.

public ActionResult Test()
        {
            CRMProfileRecord record = new CRMProfileRecord();
            record.FullName = "test";
            return View(record);
        }

        [HttpPost, ActionName("Test")]
        public string TestPost(CRMProfileRecord record)
        {
            if (string.IsNullOrEmpty(record.FullName))
                return "fail";
            else return "ok";
        }

Coordinator
Feb 22, 2012 at 3:52 AM

It;s probably that the model binder can't map because of the way you named your form fields.

Feb 23, 2012 at 9:36 AM

Thanks a lot. Now it is working. The problem is because of the way the controls are named.