开发者

MVC3 Only posted form values retained?

开发者 https://www.devze.com 2023-03-06 01:53 出处:网络
I am using strongly typed views in an MVC3 web app.I\'ve noticed that when a form is submitted, the ViewModel that is passed to the controller only has values for properties that have form elements as

I am using strongly typed views in an MVC3 web app. I've noticed that when a form is submitted, the ViewModel that is passed to the controller only has values for properties that have form elements associated with them. For instance, the example below shows a simple confirmation View with a checkbox and a phone number that the user must confirm before proceeding. When the form is submitted to the controller action, the UserConfirmed property contains a value, but the PhoneNumber property is null.

Is there any way for开发者_开发问答 the ViewModel to retain all of its values or do I have to repopulate the ViewModel properties that do not have form elements associated with them?

The View

@model WebMeterReplacement.ViewModels.Appointment.ScheduleConfirmationViewModel
@using (Html.BeginForm()) {
@Html.ValidationSummary(false)

@Html.CheckBoxFor(model => model.UserConfirmed) 
<span>Please confirm before proceeding</span>
<div>
    Phone Number: @Model.PhoneNumber
</div>
<input type="submit" value="Confirm"/>

The Controller

[HttpPost]
public ActionResult ScheduleConfirmation(ScheduleConfirmationViewModel model)
{
if (model.UserConfirmed)
{
    // add ViewModel data to repository
}
else
{
    ModelState.AddModelError("ERROR", WebResources.strERROR_ConfirmSchedule);
}

return View(model);
}


Since your writing the phonenumber as output to the page it won't be automatically posted back (you've found out that part) What you can do is populate an hidden or read-only field with the phonenumber so that it will be posted back to your controller. An second option is to make a new call to your datasource and repopulate your object before saving it back to your datasource.


I generally POST back information like this in a hidden input. I personally use this heavily to pass data needed to return the user exactly where they where before pressing edit.

In your case it's as simple as

@model WebMeterReplacement.ViewModels.Appointment.ScheduleConfirmationViewModel
@using (Html.BeginForm()) {
@Html.ValidationSummary(false)

@Html.CheckBoxFor(model => model.UserConfirmed) 
<span>Please confirm before proceeding</span>
<div>
    @Html.HiddenFor(m => m.PhoneNumber)
    Phone Number: @Model.PhoneNumber
</div>
<input type="submit" value="Confirm"/>

For future reference:

If your passing complex objects back you need one hidden field per attribute (Hiddenfor does NOT iterate)

View

WRONG
@Html.HiddenFor(m => m.PagingData)

RIGHT
@Html.HiddenFor(m => m.PagingData.Count)
@Html.HiddenFor(m => m.PagingData.Skip)
@Html.HiddenFor(m => m.PagingData.PageSize)

Action

public HomeController(AViewModel Model)
{
   PagingData PagingData = Model.PagingData;
   Skip = PagingData.Skip;
}

If your passing Arrays you can do it like this

View

@if (Model.HiddenFields != null)
{   
foreach (string HiddenField in Model.HiddenFields)
{
    @Html.Hidden("HiddenFields", HiddenField)    
}
}

Action

public HomeController(AViewModel Model)
{
    String[] HiddenFields = Model.HiddenFields;
}


Well, the form will only POST elements that you have created. As you found out, simply writing the phone number out to the page will not suffice. The model binder can only bind those properties which exist in the posted data.

Generally you have a couple of options here:

1) You can create Input elements for all of the properties in your model, using visible elements (like a textbox) for those properties you want to edit, and hidden elements which should be posted back but have no UI

2) Post back a partial representation of your model (as you are doing now), read the entity back in from it's data source (I assume you're using some kind of data source, EF maybe) and then alter the properties of that entity with the ones from your form.

Both scenarios are common but it really depends on the complexity of your model.


I know this thread is a bit old, but thought I'd resurrect it to get feed back on my solution to this.

I'm in a similar situation where my objects are passed to a view, and the view may only display part of that object for editing. Obviously, when the controller receives the model back from the default model binder, and values not posted back become null.. and saving this means that a DB value becomes null just because it wasn't displayed/returned from a view.

I didn't like the idea of creating a model for each view. I know it's probably the right way... but I was looking for a reusable pattern that can be implemented fairly quickly. See the "MergeWith" method... as this would be used to take a copy of the object from the database and merge it with the one returned from the view (posted back)

    namespace SIP.Models
{
    [Table("agents")]
    public class Agent
    {
        [Key]
        public int id { get; set; }

        [Searchable]
        [DisplayName("Name")]
        [Column("name")]
        [Required]
        [StringLength(50, MinimumLength = 4)]
        public string AgentName { get; set; }

        [Searchable]
        [DisplayName("Address")]
        [Column("address")]
        [DataType(DataType.MultilineText)]
        public string Address { get; set; }


        [DisplayName("Region")]
        [Searchable]
        [Column("region")]
        [StringLength(50, MinimumLength = 3)]
        public string Region { get; set; }


        [DisplayName("Phone")]
        [Column("phone")]
        [StringLength(50, MinimumLength = 4)]
        public string Phone { get; set; }

        [DisplayName("Fax")]
        [Column("fax")]
        [StringLength(50, MinimumLength = 4)]
        public string Fax { get; set; }


        [DisplayName("Email")]
        [RegularExpression(@"(\S)+", ErrorMessage = "White space is not allowed")]
        [Column("email")]
        [StringLength(50, MinimumLength = 4)]
        public string Email { get; set; }


        [DisplayName("Notes")]
        [Column("notes")]
        [DataType(DataType.MultilineText)]
        public string Notes{ get; set; }

        [DisplayName("Active")]
        [Column("active")]
        public bool Active { get; set; }


        public override string ToString()
        {
            return AgentName;
        }

        public bool MergeWith(Agent a, string[] fields)
        {
            try
            {
                foreach (PropertyInfo pi in this.GetType().GetProperties())
                {
                    foreach (string f in fields)
                    {
                        if (pi.Name == f && pi.Name.ToLower() != "id")
                        {
                            var newVal = a.GetType().GetProperty(f).GetValue(a,null);
                            pi.SetValue(this, newVal, null);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                return false;
                //todo: Log output to file...
            }

            return true;
        }
    }
}

And to use this in the controller.. you'd have something like..

 [HttpPost]
    public ActionResult Edit(Agent agent)
    {


        if (ModelState.IsValid)
        {
            Agent ag = db.Agents.Where(a => a.id == agent.id).ToList<Agent>().First<Agent>();
            ag.MergeWith(agent, Request.Params.AllKeys);

            db.Entry(ag).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(agent);
    }

This way, during post back, it takes the object from the database, and updates it with object from view... but only updates the values that were posted back.. So if you have a field like "address" or something that doesn't appear in the view.. it doesn't get touched during the update.

I've tested this so far and i works for my purposes, tho i welcome any feedback as I'm keen to see how others have overcome this situation. It's a first version and i'm sure it can be implemented better like through an extension method or something.. but for now the MergeWith can be copy/pasted to each model object.


Yes, Just place hidden fields in the form for those values which you are not using and want to return to server control. Thanks

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号