Pregunta

It is a create view page where i have insert supplier information and it is inserting data successfully. i want to get this data automatically when i will enter a name and it exist it will show all of the data in these field, if not exist then i will save to the table.

 @model FCBook.Supplier

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Supplier</legend>

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

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

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

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

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

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

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

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

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

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

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

My controller action to this create view is

public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Supplier/Create

        [HttpPost]
        public ActionResult Create(Supplier collection)
        {
            try
            {
                // TODO: Add insert logic here
                var db = new PetaPoco.Database("FCBook");
                if (collection != null)
                {
                    collection.Insert();
                }
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
¿Fue útil?

Solución

I think you want to look into using Remote validation. It can be triggered after a user enters a value into one of your fields. I'm using this approach to populate other fields upon such an event occurring.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top