2

I have a scenario where in I need to store multiple rows in a single table.Let me explain it in detail.

I have a table Price which has 4 columns, ID, ModelID, AppSettingID,Amount.

I am looking for inserting multiple values to the table where

  1. ID would be the Primary Key.
  2. ModelID would be same for all the rows.
  3. AppSettingID and Amount will be different for all the rows and would be based on the selection user does on the view.

I have bound the AppSettingID to different combo boxes on the view as I have it categorized in the database.

This is what I am doing right now.

View:

<div class="editor-label">
        @Html.LabelFor(model => model.ModelID, "Model")
    </div>
    <div class="editor-field">
        @Html.DropDownList("ModelID", String.Empty)
        @Html.ValidationMessageFor(model => model.ModelID)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.AppsettingID, "Mobile Condition")
    </div>

    <div class="editor-field">
        @Html.DropDownList("Mobile Condition", new       SelectList(ViewBag.ConditionID, "Text", "Value"))
        @Html.ValidationMessageFor(model => model.AppsettingID)
    </div>

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

    <div class="editor-label">
        @Html.LabelFor(model => model.AppsettingID, "Tennure")
    </div>

    <div class="editor-field">
        @Html.DropDownList("Tennure", new SelectList(ViewBag.AppsettingID, "TexT", "Value"))
        @Html.ValidationMessageFor(model => model.AppsettingID)
    </div>

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

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

Controller:

public ActionResult Create()
    {
        //ViewBag.AppsettingID = db.Appsettings.Select(r => r.ID).Distinct();
        ViewBag.ModelID = new SelectList(db.Models, "ID", "Name");
        //ViewBag.Tennure = db.Appsettings.Select(s => s.Type == "Tennure").Distinct();

        IQueryable<Appsetting>TennureIDs = db.Appsettings.Where(s => s.Type == "Tennure").Distinct();
        IQueryable<Appsetting> Conditions = db.Appsettings.Where(s => s.Type == "Mobile Condition").Distinct();

        List<SelectListItem> items = new List<SelectListItem>();
        foreach (var t in TennureIDs)
        {
            SelectListItem s = new SelectListItem();
            s.Text = t.ID.ToString();
            s.Value = t.Value.ToString();
            items.Add(s);
        }
        ViewBag.AppsettingID = items;

        List<SelectListItem> conds = new List<SelectListItem>();
        foreach (var t in Conditions)
        {
            SelectListItem s = new SelectListItem();
            s.Text = t.ID.ToString();
            s.Value = t.Value.ToString();
            conds.Add(s);
        }

        ViewBag.ConditionID = conds;

        return View();
    }

    //
    // POST: /Price/Create

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(Price price, FormCollection form)
    {
        if (ModelState.IsValid)
        {
            int test = Convert.ToInt16(form["Mobile Condition"]);
            price.AppsettingID = test;
            db.Prices.Add(price);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        //ViewBag.AppsettingID = new SelectList(db.Appsettings, "ID", "Type", price.AppsettingID);
        //ViewBag.ModelID = new SelectList(db.Models, "ID", "Name", price.ModelID);
        return View(price);
    }
4
  • Are you need to save the table rows which is in the razor to database? Commented May 8, 2015 at 5:42
  • @Golda, Yes from controller to Database. Commented May 11, 2015 at 17:17
  • How will you add a new rows to the model? Commented May 12, 2015 at 6:05
  • Please read this interesting post Commented Nov 9, 2015 at 8:40

1 Answer 1

4

I hope this solution will help you

The following is a razor page, here I have displayed the model in table format

@model List<MyModel>

<h2>Create</h2>

@using (Html.BeginForm())
{
    <table>
        <tr>
            <th>
                Model ID
            </th>
            <th>
                App Setting ID
            </th>          
            <th>
                Amount
            </th>            
        </tr>

        @for (int i = 0; i < Model.Count; i++)
        {
            <tr>
                <td>
                    @Html.TextBoxFor(x => Model[i].ModelID)
                </td>
                <td>
                    @Html.TextBoxFor(x => Model[i].AppsettingID)
                </td>
                <td>
                    @Html.TextBoxFor(x => Model[i].Amount)
                </td>
            </tr>                    
        }
    </table>
    <input type="submit" />
}

When user clicks the submit button, it will pass the model to the controller

Controller

    [HttpPost]
    public ActionResult Create(List<MyModel> m)
    {
        // Do Coding
        return View("Create", m);
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.