简体   繁体   中英

Model properties are null on postback

I am creating a view which displays a simple 3 field form. The values retrieved from the controller via my database tier are null, so the form field show up as empty, which is as expected.

The issue I am having is, if I leave the 3 form fields blank and click Save, when it comes to calling the stored procedure to update the row (these 3 fields are nullable in the database) I get an exception as the stored procedure does not like the null.

Here is my model:

public class UserModel
{
    [DisplayName("Campaign Name")]
    public string CampaignName { get; set; }
    [DisplayName("User Owner Name")]
    public string UserOwnerName { get; set; }
    [DisplayName("Lead Status")]
    public string LeadStatus { get; set; }
}

Controller:

[HttpPost]
public ActionResult OptionalFields(Models.UserModel model)
{
    var businessLayerFunctions = new BL.Functions();
    businessLayerFunctions.UpdateConfig(model);
    return View();
}

View:

@using (Html.BeginForm("OptionalFields", "Home"))
{
    @Html.HiddenFor(m => m.ConfigID)
    @Html.LabelFor(m => m.CampaignName)
    @Html.TextBoxFor(m => m.CampaignName)
    @Html.LabelFor(m => m.LeadStatus)
    @Html.TextBoxFor(m => m.LeadStatus)
    @Html.LabelFor(m => m.UserOwnerName)
    @Html.TextBoxFor(m => m.UserOwnerName)
    <p><input type="submit" id="optionalfields" value="Save" /></p>
}

My question is, if these fields are empty when I submit them, would they not bind to the model as "" ?

The Request.Form inside the controller method is

{ConfigID=123&CampaignName=&LeadStatus=&UserOwnerName=}

Is the fields being null by design? As it stands I will have to check for the null and assign a blank string to make the stored procedure work.

My stored procedure is just a one liner

UPDATE dbo.table 
    SET UserOwnerName = @userOwnerName, 
        LeadStatus = @leadStatus, 
        CampaignName = @campaignName, 
        LastModifiedDate = @lastModifiedDate 
    WHERE ConfigID = @configID

And I add these parameters like so

comm.Parameters.AddWithValue("@configID", configID);
comm.Parameters.AddWithValue("@userOwnerName", userOwnerName);
comm.Parameters.AddWithValue("@leadStatus", leadStatus);
comm.Parameters.AddWithValue("@campaignName", campaignName);
comm.Parameters.AddWithValue("@lastModifiedDate", DateTime.UtcNow);

Are you saying I should check if they are null here? And if so add DBNull.Value?

The model binder normalizes empty strings to null so your problem is not there. If you debug your code, you will most likely see the fields are indeed set to null.

What you need to do is check the strings before adding them as parameters and use DBNull.Value instead. Something like this:

var param = !string.IsNullOrEmpty(campaignName) ?
            new SqlParameter("campaignName", campaignName) :
            new SqlParameter { ParameterName = "campaignName", SqlDbType = SqlDbType.NVarChar, Value = DBNull.Value };

I had null model properties when I used "Generate property..." to create them.

The code generator made the 'set' method an 'internal' set:

public string MyProperty { get; internal set; }

Removing 'internal' allowed it to get set through the postback:

public string MyProperty { get; set; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM