模型绑定中的自定义类型转换(ASP.NET MVC)

时间:2017-07-26 00:35:30

标签: asp.net-mvc type-conversion model-binding custom-model-binder

我有一个提交多个字符串值的表单。我的模型有一种自定义类型(" Service")。在尝试将字符串值转换为" Service"时,应用程序会出错。我是MVC的新手。感谢您的帮助!

控制器(订单/创建):

    // POST: Orders/Create
    [HttpPost]
    public ActionResult Create([Bind(Include = "Id,Name,Email,Language,Service,DocumentPath,Content,DeliveryUrgent,Cost,Payment")] Order order)
    {
        var errors = ModelState.Values.SelectMany(v => v.Errors);
        if (ModelState.IsValid)
        {
            // some stuff

            return View();
        }

        return View(order);
    }

模型(订单):

public class Order
{
    [Key]
    public int Id { get; set; }
    public string Name { get;set;}
    public string Email { get; set; }        
    public string Language { get; set; }                
    public string DocumentPath { get; set; }
    public string Content { get; set; }        
    public bool DeliveryUrgent { get; set; }
    public float Cost { get; set; }
    public bool Payment { get; set; }       
    public virtual Service Service { get; set; }
}

模特(服务):

public class Service
{
    [Key]
    public int Id { get; set; }
    public string Title { get; set; }
    public double CostPerWord { get;set;}
    public int Discount { get; set; }  
    public int MinimumCost { get; set; }
    public int UrgentCost { get; set; }

    [JsonIgnore]
    public virtual ICollection<Order> Orders { get; set; }
}

查看(创建):

@model testapp.Models.Order

@{
    ViewBag.Title = "Test";
}

<div class="jumbotron text-center">
    <h1>testapp</h1>
</div>

<div class="row" id="app">
    <div class="col-md-offset-1 col-md-10">
        <form class="form-horizontal">
            <div class="row" style="margin-bottom:10px;">
                <div class="col-xs-8" style="margin-top:10px;">

                    <transition name="slide-down" appear mode="out-in">
                        <strong v-if="form.filePath.length == 0" style="padding-left: 5px; padding-bottom: 5px;">Enter your text:</strong>
                    </transition>
                </div>
                <div class="col-xs-4 text-right">or <button type="button" v-on:click="selectDocument" href="#" class="btn btn-primary" style="padding: 5px 15px">Upload file</button></div>
                <div style="display: none;">
                    <input type="file" ref="file_doc" class="form-control" id="file_doc" name="file_doc" v-on:change="uploadDocument" accept=".txt,.pdf,.doc,.docx" />
                </div>
            </div>
            <div class="form-group" style="margin-bottom:0px;">
                <div class="alert alert-info" v-show="fileUploadProgress < 100 && fileUploadProgress > 0">
                    Uploading: {{fileUploadProgress}}%
                </div>
                <textarea v-if="form.filePath.length == 0" v-model="form.content" ref="txt_content" name="content" style="margin: 0 2%; width: 96%;" class="form-control full-width" autofocus="autofocus" rows="15" placeholder="Enter your text here..."></textarea>
                <div v-else class="full-width" style="text-align: center; padding: 50px 25px; background-color: #f3f9ec; border: 1px solid #aaa;">
                    <p class="full-width" style="font-size: 2em">File Uploaded!</p>
                    <p>
                        <button class="btn btn-sm btn-default" type="button" v-on:click="clearFile">
                            <span class="glyphicon glyphicon-remove"></span>
                            Clear file
                        </button>
                    </p>
                </div>
            </div>
            <div class="row">
                <div class="col-xs-8" style="font-size: 2em" v-bind:class="{'not-visible': !(form.content.length || form.fileContentSample.length)}">
                    <span class="text-muted">Language: </span>
                    <span class="text-success" v-if="isValidLanguage && !loading">{{catalog.languages[form.language]}}</span>
                    <span class="text-info" v-else-if="form.wordCount > 0 && loading">Verifying...</span>
                    <span class="text-danger" v-else-if="form.wordCount > 0 && !loading">Not English</span>
                </div>
                <div class="col-xs-4 text-right" style="font-size: 2em">
                    <span class="text-muted">Words:</span>
                    <span class="text-success" v-if="form.wordCount > 0">{{form.wordCount}}</span>
                    <span class="text-danger" v-else>0</span>
                </div>
            </div>
            <br /><br />
            @*<h4 class="text-center text-muted" style="margin-bottom: 13px; font-size: 1.7em;"><i>Choose Service:</i></h4>*@
            <div class="row">
                <div class="col-xs-6">
                    <button type="button" v-on:click="setService('translation')" class="btn-service" v-bind:class="{'selected': form.service == 'translation'}" :disabled="!isValidLanguage">
                        <span class="glyphicon glyphicon-refresh"></span>
                        <span>Translation</span>
                        <div class="sticker-orange">
                            <span>${{price.translation.new}}</span>
                            <span class="muted">was ${{price.translation.old}}</span>
                        </div>
                    </button>
                </div>
                <div class="col-xs-6">
                    <button type="button" v-on:click="setService('proofreading')" class="btn-service" v-bind:class="{'selected': form.service == 'proofreading'}" :disabled="!isValidLanguage">
                        <span class="glyphicon glyphicon-search"></span>
                        <span>Proofreading</span>
                        <div class="sticker-orange">
                            <span>${{price.proofreading.new}}</span>
                            <span class="muted">was ${{price.proofreading.old}}</span>
                        </div>
                    </button>
                </div>
            </div>
            <br /><br />
            <div class="bg-light-gray section-padding text-center">
                <div class="checkbox" style="margin-bottom: 15px; font-weight:bold;">
                    <label>
                        <input type="checkbox" value="true" v-model="form.urgent" :disabled="!isFormValid"> 2 Days delivery ($10)
                    </label>
                </div>
                <p class="lead">
                    Total: <strong>${{price.total}}</strong>
                </p>
                <button type="button" v-on:click="checkFormValid" class="btn btn-lg btn-primary" style="font-size:larger; padding: 15px 30px;" :disabled="!isFormValid">
                    <span class="glyphicon glyphicon-ok-circle"></span>
                    Pay Now
                </button>
            </div>
            <br /><br />
            <transition name="slide-down" appear mode="out-in">
                <div v-show="isFormValid && isPayNowClicked">
                    <div class="form-group">
                        <label for="name" class="col-sm-offset-2 col-sm-2 control-label">Name:</label>
                        <div class="col-sm-8">
                            <input type="text" v-model="form.name" ref="txt_name" class="form-control" name="name" placeholder="Your Name">
                            <span class="text-danger" v-if="!userInfoStatus.name.valid && userInfoStatus.name.dirty">Please enter a valid name.</span>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="email" class="col-sm-offset-2 col-sm-2 control-label">Email:</label>
                        <div class="col-sm-8">
                            <input type="email" v-model="form.email" class="form-control" name="email" placeholder="Email address">
                            <span class="text-danger" v-if="!userInfoStatus.email.valid && userInfoStatus.email.dirty">Please enter a valid email address.</span>
                        </div>
                    </div>
                    <br />
                    <div class="text-center">
                        <strong style="margin-bottom: 17px; font-size: 1.7em;">Pay with</strong>
                        <br />
                        <div class="btn-group-lg">
                            <button class="btn btn-lg btn-primary" type="button" v-on:click="submit('paypal')" :disabled="!isUserInfoValid">Paypal</button>
                            <button class="btn btn-lg btn-primary" type="button" v-on:click="submit('square')" :disabled="!isUserInfoValid">Square</button>
                        </div>
                    </div>
                </div>
            </transition>
        </form>
    </div>
</div>

@Scripts.Render("~/bundles/axios")
@Scripts.Render("~/bundles/vue")
@Scripts.Render("~/bundles/site")

错误:

  

&#34;参数转换类型&#39; System.String&#39;输入   &#39; testapp.Models.Service&#39;失败因为没有类型转换器可以转换   这些类型之间。&#34;

enter image description here

0 个答案:

没有答案