English 中文(简体)
通过淘汰向MVC行动提交的表格有额外的双重报价
原标题:Form submitted to MVC action via knockout has extra double quotes

我对通过一个击倒模型提交给我的控制器操作的数据有疑问。每个字符串字段都有额外的双重报价,这是一个问题。

虽然我可以人工解密数据,但似乎在使用MVC3时根本不应该出现这一问题。

Html :

@inherits System.Web.Mvc.WebViewPage<FbWizardCreateTabModel>

@using (Html.BeginForm("InstallApplication", "FbWizard", FormMethod.Post, new { id = "createtab", @data_bind = "submit:onSubmit" }))
{
    <p>Page Id: <span data-bind="text: PageId"></span></p>
    <p>Page Name: <span data-bind="text: PageName"></span></p>
    <p>Tab Name: @Html.TextBoxFor(m => m.TabName, new { data_bind = "value: TabName" })</p>
}

<button class="btn btn-primary next">Submit</button>

脚本代码 :

 <script type="text/javascript">

        var initialData = @Html.Raw(Model.JsonData);
        var viewModel = ko.mapping.fromJS(initialData);

        viewModel.onSubmit = function() {

            var action = $( #createtab ).attr( action );

            ko.utils.postJson(action, this);

            return false;
        };

        ko.applyBindings(viewModel);

    </script>

主计长行动:

[HttpPost]
public ActionResult InstallApplication(FbWizardCreateTabModel model)
{
    // The model is mangled at this point, see image below

    return View();
}

之后的模型内容 :

""https://i.sstatic.net/GB0XQ.png" alt="此处输入图像描述"/ >

原始日志数据 :

POST http://mysite.localhost:7785/Admin/FbWizard/InstallApplication HTTP/1.1
Host: mysite.localhost:7785
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Referer: http://mysite.localhost:7785/Admin/FbWizard/CreateTab
Cookie: fbsr_....
Content-Type: application/x-www-form-urlencoded
Content-Length: 333

PageId=%22231271443653720%22&PageName=%22Car5%22&TabName=%22Auctions2%22&JsonData=null&__ko_mapping__=%7B%22include%22%3A%5B%22_destroy%22%5D%2C%22ignore%22%3A%5B%5D%2C%22copy%22%3A%5B%5D%2C%22mappedProperties

我做错什么了?

最佳回答

我可能离基地很远,但「强者」(pussibly

   postJsonNoQuotes = function (urlOrForm, data, options) {
        options = options || {};
        var params = options[ params ] || {};
        var includeFields = options[ includeFields ] || this.fieldsIncludedWithJsonPost;
        var url = urlOrForm;

        // If we were given a form, use its  action  URL and pick out any requested field values
        if((typeof urlOrForm ==  object ) && (ko.utils.tagNameLower(urlOrForm) === "form")) {
            var originalForm = urlOrForm;
            url = originalForm.action;
            for (var i = includeFields.length - 1; i >= 0; i--) {
                var fields = ko.utils.getFormFields(originalForm, includeFields[i]);
                for (var j = fields.length - 1; j >= 0; j--)
                    params[fields[j].name] = fields[j].value;
            }
        }

        data = ko.utils.unwrapObservable(data);
        var form = document.createElement("form");
        form.style.display = "none";
        form.action = url;
        form.method = "post";
        for (var key in data) {
            var input = document.createElement("input");
            input.name = key;
           // I think this is the offending line....
           // input.value = ko.utils.stringifyJson(ko.utils.unwrapObservable(data[key]));
            input.value = ko.utils.unwrapObservable(data[key]);
            form.appendChild(input);
        }
        for (var key in params) {
            var input = document.createElement("input");
            input.name = key;
            input.value = params[key];
            form.appendChild(input);
        }
        document.body.appendChild(form);
        options[ submitter ] ? options[ submitter ](form) : form.submit();
        setTimeout(function () { form.parentNode.removeChild(form); }, 0);
    }
}
问题回答

此密钥位于函数名称 postsJson 。 它不是要用它做什么, 它需要您给它的对象上的任何属性, 并把它们的值标为 JSON 。 例如, 您可以将您的职位更改为此 :

ko.utils.postJson(action, { json: this });

以及你们为此采取的行动:

[HttpPost]
public ActionResult InstallApplication(string json)
{
    JavascriptSerializer ser = new JavascriptSerializer();
    FbWizardCreateTabModel model = ser.Deserialize<FbWizardCreateTabModel>(json);

    return View();
}

如果你想要使用Keith建议的函数, 我会让JSON出名, 因为您似乎不想实际张贴 JSON, 也许称之为 postaAsFormData





相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...

热门标签