0

To put simply, I am scanning a drivers license with a magnetic strip card reader, using javascript RegEx to get the data I need, sending that data to the server as JSON via an ajax call, and then sending back a partial view with an Ajax.BeginForm with the data filled out.

In fact, all of this is working great. The problem is that I'm trying to alter the data on the server in C# before sending it to the view, but no matter what I do the original non-formatted data is showing up on the page still. I even stepped through the entire process, and looking at the Model data for the view shows that it received the correct data. Yet when the view is displayed, the non-formatted data (which does not actually exist in the Model object when I browse it) is what shows up in the text boxes.

function LoadVisitorCreatePartial(CardData) {
$.post(webroot + "Visitor/GetVisitorLogCreateVisitorForm", CardData, function (data) {
    $('#visitor-info').append(data);
});

}

Above is the relevant javascript I am using for the call. I am using Carl Raymond's jquery card swipe to pull the data and I implemented some of my own RegEx to get the data I need for "CardData", which is JSON.

[HttpPost]
public ActionResult GetVisitorLogCreateVisitorForm(DriversLicenseForm CardData)
{
    var form = visitorService.GetForm(CardData);
    return PartialView("Form", form);
}

Above is the controller function that is being called. DriversLicenseForm is essentially the JSON object, as a C# class.

public VisitorForm GetForm(DriversLicenseForm CardData)
{
    var textInfo = CultureInfo.InvariantCulture.TextInfo;
    var DateOfBirth = DriversLicense.ParseDriversLicenseDate(CardData.DateOfBirthString);

    CardData.FirstName = ConvertToTitleCase("i'm ron burgundy?"); 
    CardData.LastName = textInfo.ToTitleCase(CardData.LastName);
    var form = new VisitorForm();
    if (DateOfBirth != null)
    {
        CardData.DateOfBirth = Convert.ToDateTime(DateOfBirth);
    }
    form = Mapper.Map<VisitorForm>(CardData);
    form.ID = -1;
    form = SetFormProperties(form);
    return form;
}

In the above code you can see that I was getting annoyed. I decided to manually set the name and despite me setting CardData.FirstName to something completely different (verified when stepping through in debug mode) when the form comes back, it just shows my name from drivers license. I also have two title case functions because initially I thought that was the problem and found a non-textinfo solution, though that changed nothing and after debug stepping through, the data in CardData is being changed and updated properly. I feel it is worth noting one more time that the data is changed in debug mode in CardData and in the form after AutoMapper maps it out, my code appears to be working fine, except the actual view displays the original data despite there being no reference or reason for it to even know that data exists.

I literally have no idea what to do at this point.

EDIT: Here is the view code

@using (Ajax.BeginForm("PostForm", "Visitor", new AjaxOptions { HttpMethod = "POST",     UpdateTargetId = "visitor-info", OnSuccess = "VisitorPostback" }))
{
@Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    @Html.HiddenFor(model => model.ID)

    <div class="editor-label">
        @Html.LabelFor(model => model.LastName)
    </div>
    <div class="editor-field">
        @Html.TextBoxFor(model => model.LastName, new { @class = "focus" })
        @Html.ValidationMessageFor(model => model.LastName)
    </div>
<p>
    <input type="submit" class="btn btn-primary" value="Save" />
</p>
}

I cut out a lot of the actual textboxes, but left one as an example. They are all done the same way.

4

1 回答 1

1

将数据发送到 MVC Post 操作方法时,ModelState 将填充您发送的数据。例如,当发布的表单出现验证错误时,您可以简单地返回视图,并且所有用户表单输入以及验证错误都会显示出来。

通常在 POST 之后,您会将 (PRG -> Post Redirect Get) 重定向到另一个操作方法,但看起来您无法在这里轻松地做到这一点。

看起来这些 ModelState 值被保留在“表单”视图的返回上,并将它们绑定到匹配的表单元素。尝试添加ModelState.Clear()到您的 POST 操作方法中,看看这是否是导致问题的原因。

[HttpPost]
public ActionResult GetVisitorLogCreateVisitorForm(DriversLicenseForm CardData)
{
    ModelState.Clear();
    var form = visitorService.GetForm(CardData);
    return PartialView("Form", form);
}
于 2013-11-13T18:48:24.913 回答