Search code examples
htmlasp.net-mvc-4asp.net-mvc-controller

Auto save a drop down list


I have a drop down list for feed back. I save the data when save button is pressed. I want to auto save the drop down list without using save button how i can do it? here is my ontroller

    public ActionResult SelectFeedBack(int id)
    {
        YelloAdminDbContext db = new YelloAdminDbContext();
        ViewBag.FeedBack = new SelectList(db.FeedBack, "FeedBackId", "FeedBackDrpDown");

        return PartialView();


    }
    [HttpPost]
    public ActionResult SelectFeedBack(int FeedBack, int id)
    {
        YelloAdminDbContext db = new YelloAdminDbContext();
        if (ModelState.IsValid)
        {
            var temp = db.FeedBack.Find(FeedBack);
            db.SaveFeedBack.Add(new SaveFeedBack { LoginId = id, feedback = temp });
            db.SaveChanges();
            return JavaScript("alert ('success');");
        }
        return JavaScript("alert ('error');");
    }
    public ActionResult DisplayFeedBack(int id)
    {
        YelloAdminDbContext db = new YelloAdminDbContext();
        var Feed = db.SaveFeedBack.Where(x => x.LoginId == id).FirstOrDefault();
        if (Feed == null)
        {
            return Content("No Feed Back Entered");
        }
        return Content(Feed.feedback.FeedBackDrpDown);
    }

and my view is

  @model MyYello.Admin.Models.FeedBack
 @{
ViewBag.Title = "Feed Back";
  }

  <h2>Add Notes</h2>

   @using (Html.BeginForm("SelectFeedBack", "Admin", FormMethod.Post))
   {@Html.ValidationSummary(true);
<fieldset>
    @Html.HiddenFor(item => item.FeedBackId)
    <legend>Create Notes</legend>
    <div class="editor-label">
        @Html.LabelFor(item => item.FeedBackDrpDown)
    </div>

        @Html.DropDownList("FeedBack")



    <p><input type="Submit" value="Save" id="Save" /></p>

</fieldset>

 }

How i can save the data without using button.


Solution

  • Use some event like focusout which in turn will auto submit the form .

    $(this.document).ready(function () {
        var form = $("form");
         form.submit();
    });