NickP
NickP

Reputation: 1414

Default value on one of the fields in create view

I am trying to set a default value for the scaffold create view that MVC created for me. I have tried creating an instance of the model and passing it to the view but its throwing errors.

  Function Create(id As Integer) As ViewResult
        Dim job As Job
        job.CustomerId = id


        Return View(job)
    End Function

How would I get my create view to be prepopulated with the id in the parameter using the following view so when a new Job is created it is automatically created with that customer id. My view is below:

        @ModelType Laundry.Job

        @Code
            ViewData("Title") = "Create"
        End Code

        <h2>Create</h2>

        <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
        <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

        @Using Html.BeginForm()
            @Html.ValidationSummary(True)
            @<fieldset>
                <legend>Job</legend>

                <div class="editor-label">
                    @Html.LabelFor(Function(model) model.JobDate)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(Function(model) model.JobDate)
                    @Html.ValidationMessageFor(Function(model) model.JobDate)
                </div>

                <div class="editor-label">
                    @Html.LabelFor(Function(model) model.CustomerId)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(Function(model) model.CustomerId)
                    @Html.ValidationMessageFor(Function(model) model.CustomerId)
                </div>

                <div class="editor-label">
                    @Html.LabelFor(Function(model) model.BillId)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(Function(model) model.BillId)
                    @Html.ValidationMessageFor(Function(model) model.BillId)
                </div>

                <div class="editor-label">
                    @Html.LabelFor(Function(model) model.JobStatus)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(Function(model) model.JobStatus)
                    @Html.ValidationMessageFor(Function(model) model.JobStatus)
                </div>

                <div class="editor-label">
                    @Html.LabelFor(Function(model) model.JobAmount)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(Function(model) model.JobAmount)
                    @Html.ValidationMessageFor(Function(model) model.JobAmount)
                </div>

                <p>
                    <input type="submit" value="Create" />
                </p>
            </fieldset>
        End Using

        <div>
            @Html.ActionLink("Back to List", "Index")
        </div>

I don't necessarily even need to show the customer id on the form but that new object needs to be created with the parameter passed to the controller and the rest of the info the user chooses.

Upvotes: 0

Views: 170

Answers (1)

Yasser Shaikh
Yasser Shaikh

Reputation: 47804

Method 1 : If you want to show the customerId, then you can use the following code...

<div class="editor-label">
 @Html.LabelFor(Function(model) model.CustomerId)
</div>

This way, the CustomerId will be displayed, user cannot change it and when postback is done, this value is still present as a part of the model.

Method 2 : If you dont want to display, but still use it for the above purpose, then use the code written below...

<div class="editor-label">
 @Html.HiddenFor(Function(model) model.CustomerId)
</div>

Hope this is what you wanted.

Upvotes: 1

Related Questions