0

I have a form that submits several string values. My model has one custom type ("Service"). Application gives error while trying to convert a string value to "Service". I am new to MVC. I appreciate your help!

Controller (Orders/Create):

    // 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);
    }

Model (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; }
}

Model (Service):

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; }
}

View (Create):

@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")

The error:

"The parameter conversion from type 'System.String' to type 'testapp.Models.Service' failed because no type converter can convert between these types."

enter image description here

Ned
  • 1,055
  • 9
  • 34
  • 58
  • You need to show your view - in particular for the `Service` property (you need to create a form input for each property of `Service`) –  Jul 26 '17 at 00:38
  • added @StephenMuecke – Ned Jul 26 '17 at 00:52
  • Its not from what you have shown (why are you manually generating the html instead of using the strongly typed `HtmlHelper` methods). What are the inputs you have relating to `Service` that your editing? - you would need `@Html.EditorFor(m => m.Service.Title)` (ditto for all other properties of `Service`) in order to bind –  Jul 26 '17 at 01:30
  • Input for "Service" is string (either "translation" or "proofreading"). However, it should go to Service.Title. I think I am trying to bind it to entire Service class... Where should I place `@Html.EditorFor(m => m.Service.Title)` ? – Ned Jul 26 '17 at 01:35
  • The problem seems come from this property: `public virtual Service Service { get; set; }`. Try rename the model binding e.g. `public virtual Service Services { get; set; }` & see if it works. – Tetsuya Yamamoto Jul 26 '17 at 01:39
  • Anywhere between your `
    ` tags
    –  Jul 26 '17 at 01:39

0 Answers0