Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6248135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:00:01+00:00 2026-05-24T13:00:01+00:00

Here is my question i ve a Create View i wanna add a new

  • 0

Here is my question i ve a Create View
i wanna add a new item into my linqtosql database and fill my selectlist with updated database items in it and i want it to happen instantly when i click “+” button near my input its value will be saved and added into my selectlist. How can i achive this. I was thinking on json usage but couldnt figure it out.

MY Controller

    public JsonResult Productlist(string ipro, Product prod, int productId)
    {
        prod.name = ipro;
        db.Products.InsertOnSubmit(prod);
        db.SubmitChanges();
        int pro = Convert.ToInt32(db.Products.Where(x => x.name == ipro).Select(x => x.id).Single());
        IEnumerable<SelectListItem> ProductItems = db.Products.Where(d => d.id == pro).AsEnumerable().Select(c => new SelectListItem()
        {
            Text = c.name,
            Value = c.id.ToString()
        });
        SelectList data = new SelectList(ProductItems, "Value", "Text");
        return Json(data, JsonRequestBehavior.AllowGet);
    }

    //
    //Fill Design List..
    public JsonResult GetDesigns(int productId)
    {
        IEnumerable<SelectListItem> DesignItems = db.Designs.Where(c => c.master_id == productId).AsEnumerable().Select(c => new SelectListItem()
        {
            Text = c.name,
            Value = c.id.ToString()
        });
        SelectList des = new SelectList(DesignItems, "Value", "Text");
        return Json(des, JsonRequestBehavior.AllowGet);
    }

My View (jquery)

(jquery)

<script type="text/javascript" src="@Url.Content("~/Scripts/jquery-1.5.1.js")"></script>
<script type="text/javascript" charset="utf-8">
    $("#ProductSub").click(function () {
        if ($("#iProduct").val() == "") {
            $("#iProduct").css({ backgroundColor: '#FFBBBB' });
        } else {
            $("#iProduct").css({ backgroundColor: '' });
            var inpro = $("#iProduct").val();
            $getJSON('@Url.Content("~/Admin/ProductSubmit/")', { ipro: inpro }, function (data) {
                $("select#Product").empty();
                $('select#Product').append('<option value="0">Please Select Design</option>');
                $.each(data, function (i, c) {
                    $('select#Product').append('<option value="' + c.Value + '">' + c.Text + '</option>');
                })
                $("select#Product option:first").attr('selected', 'selected');
                $("select#Product").change(productlist);
            })
        }
    });

    function productlist() {
        var prod = $("select#Product option:selected").val();
        if (prod == "" || prod == 0) {
            $("select#Design").attr('disabled', 'true');

        } else {
            $.getJSON('@Url.Content("~/Admin/GetDesigns/")', { productId: prod }, function (data) {
                $("select#Design").empty();
                $("select#Design").append('<option value="0">Please Select Design</option>');
                $.each(data, function (i, c) {
                    $('select#Design').append('<option value="' + c.Value + '">' + c.Text + '</option>');
                })
                $("select#Design").removeAttr('disabled');
                $("select#Design option:first").attr('selected', 'selected');
            })
        }
    }
</script>

View

<fieldset>
        <legend>Create New(All Fields Required)</legend>

        <div class="editor-label">Product</div>
        <div class="editor-field">
            <input id="iProduct" type="text"/>  
            @Html.ValidationMessageFor(model => model.Products.name )
        </div>
            @Html.DropDownList("Product", new SelectList((System.Collections.IEnumerable)ViewData["Productlist"], "id", "name"),"Please Select Product", new { onchange = "productlist()", style = "width:190px; padding:4px; margin:4px;" })
            <input id="ProductSub" type="button" style="padding:5px; margin-top:15px;" value="+" />

        <div class="editor-label">Design</div>
        <div class="editor-field">
            <input id="iDesign" type="text"/>  
            @Html.ValidationMessageFor(model => model.Design.name)
        </div>
            <select id="Design" style="width:190px; padding:4px; margin:4px;" onchange="designlist()"  >
            <option label="Please Select Design" selected="selected"></option>
            </select>
</fieldset>

Cause of im new around it doesnt allow me to put a picture about situation so i putted a link instead shows my page..

http://i51.tinypic.com/33ejvvb.jpg

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T13:00:03+00:00Added an answer on May 24, 2026 at 1:00 pm

    Controller

    public class AdminController : Controller
    {
        public linqVipDataContext db = new linqVipDataContext();
    
    
        public JsonResult ProductSubmit(string ipro, Product prod)
        {
            prod.name = ipro;
            db.Products.InsertOnSubmit(prod);
            db.SubmitChanges();
            int pro = Convert.ToInt32(db.Products.Where(x => x.name == ipro).Select(x => x.id).Single());
    
            /*After saving changes to the db we create a new list to see the change instantly on page.*/
    
            IEnumerable<SelectListItem> Items = db.Products.Where(d => d.id == pro).AsEnumerable().Select(c => new SelectListItem()
            {
                Text = c.name,
                Value = c.id.ToString()
            });
            SelectList data = new SelectList(Items, "Value", "Text");
            return Json(data, JsonRequestBehavior.AllowGet);
        }
    
        public JsonResult GetDesigns(int productId)
        {
            /*Here we create a list that will be posted as Json data*/
    
            IEnumerable<SelectListItem> Items = db.Designs.Where(c => c.master_id == productId).AsEnumerable().Select(c => new SelectListItem()
            {
                Text = c.name,
                Value = c.id.ToString()
            });
            SelectList data = new SelectList(Items , "Value", "Text");
            return Json(data, JsonRequestBehavior.AllowGet);
        }
    }
    

    View(Jquery)

    /*-----New Entry Buttons-----*/
    
    function productsub() {
    
        var prod = $("#iProduct").val();
    
        if (prod == "" || prod == " ") {
    
            $("input:text#iProduct").css({ 'background-color': '#FFBBBB' });
    
        } else {
    
            $('input:text#iProduct').css({ 'background-color': 'white' });
            var inpro = $("input:text#iProduct").val();
    
            $getJSON('@Url.Content("~/Admin/ProductSubmit/")', { ipro: inpro }, function (data) {
    
                $("select#Product").empty();
                $('select#Product').append('<option value="0">Please Select Product</option>');
    
                $.each(data, function (i, c) {
    
                    $('select#Product').append('<option value="' + c.Value + '">' + c.Text + '</option>');
    
                })
    
                $("select#Product option:first").attr('selected', 'selected');
                $("select#Product").change(productlist);
            })
        }
    }
    
    /*-----DropdownLists-----*/
    
    function productlist() {
    
        var prod = $("select#Product option:selected").val();
    
        if (prod == "" || prod == 0) {
    
            $("select#Design").attr('disabled', 'true');
    
        } else {
    
            $.getJSON('@Url.Content("~/Admin/GetDesigns/")', { productId: prod }, function (data) {
    
                $("select#Design").empty();
                $("select#Design").append('<option value="0">Please Select Design</option>');
    
                $.each(data, function (i, c) {
    
                    $('select#Design').append('<option value="' + c.Value + '">' + c.Text + '</option>');
    
                })
    
                $("select#Design").removeAttr('disabled');
                $("select#Design option:first").attr('selected', 'selected');
                $("select#Design").change(designlist);
    
            })
        }
    }
    

    View (HTML)

    <fieldset>
            <legend>Create New(All Fields Required)</legend>
    
            <div class="editor-label">Product</div>
            <div class="editor-field">
                <input id="iProduct" type="text"/>  
                @Html.ValidationMessageFor(model => model.Products.name )
            </div>
                @Html.DropDownList("Product", new SelectList((System.Collections.IEnumerable)ViewData["Productlist"], "id", "name"),"Please Select Product", new { onchange = "productlist()", style = "width:190px; padding:4px; margin:4px;" })
                <input id="ProductSub" type="button" style="padding:5px; margin-top:15px;" value="+" />
    
            <div class="editor-label">Design</div>
            <div class="editor-field">
                <input id="iDesign" type="text"/>  
                @Html.ValidationMessageFor(model => model.Design.name)
            </div>
                <select id="Design" style="width:190px; padding:4px; margin:4px;" onchange="designlist()"  >
                <option label="Please Select Design" selected="selected"></option>
                </select>
    </fieldset>
    

    So this is how it happens actually I was perfectly going on with the code till I see my real problem is calling the buttons function in jQuery.

    So if you have a problem with $(#Itemid).click(function() { } );
    than try using function ItemFunc() { } and call function with onchange="ItemFunc()" on your html list..

    Hope it’s useful.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have used this question here on stackoverflow to create a random string without
Newbie question here! I'm building a simple application that allows users to create and
Here's my question: I need to write a wizard, for customers to create a
Kind-of a crazy question here... I have a view display that's set up as
Apologies for the vague question. Here it is: I have a table object created
first question here. I'm developing a program in C# (.NET 3.5) that displays files
Simple question here: is there any way to convert from a jagged array to
C# question here.. I have a UTF-8 string that is being interpreted by a
Newbie question here. I'm in the beginning stages of laying out a site in
The question here is whether something like this already exists or, if not, whether

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.