Please can you help in my code and I am beginner please help. I want to call public ActionResult EditCompany(Company _Company) when I click submit button, to update the changes of the company details in the viewfile.
Hi,
<div>
I am a beginner . I have used dropdownlistbox and Submit button both have to httpost Action method. But I dont know how to give Httpost for dropdown and submit
Please help using my code given below
Company.cs
namespace BusinessLayer
{
public class Company
{
[Required]
public string Code { get; set; }
[Required]
public string CompanyName { get; set; }
[Required]
public string Address1 { get; set; }
}
//*******************
CompanyBL
public void EditCompany(Company _company)
{
string connectionString =
ConfigurationManager.ConnectionStrings["DataContext"].ConnectionString;
using (SqlConnection con = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand("spEditCompany", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter paramCode = new SqlParameter();
paramCode.ParameterName = "@Code";
paramCode.Value = _company.Code;
cmd.Parameters.Add(paramCode);
public ActionResult EditCompany()
{
CompanyBL _companybl = new CompanyBL();
IEnumerable<Company> companies = _companybl.ListCompany;
ViewBag.Companies = new SelectList(companies, "Code", "CompanyName");
return View();
}
[HttpPost]
public ActionResult EditCompany(string Companies) // This should be worked when I select company code dropdownlist
{
Company _company = new Company();
CompanyBL _companybl = new CompanyBL();
IEnumerable<Company> companies = _companybl.ListCompany;
ViewBag.Companies = new SelectList(companies, "Code", "CompanyName");
_company = _companybl.GetcompanyById(Companies);
return View(_company);
}
public ActionResult EditCompany(Company _Company) // This should be worked when I click submit button
{
if (ModelState.IsValid)
{
CompanyBL _companybl = new CompanyBL();
_companybl.EditCompany(_company);
return RedirectToAction("Index");
}
return View();
Due to you use $('form').submit(); ,so when you select the dropdownlist and click the submit button ,it all submit the form ,
and the default @using (Html.BeginForm()) is HttpPost action way to submit form;
So I suggest that you could use anther way to achieve :
$('#Companies').change(function () {
window.location.href= '@Url.Action("ControllerName","EditCompany", new {Companies ="something"})'; // it is HttpGet way to call the actionResult;
}
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Member
134 Points
616 Posts
how to call httpost editcompany for dropdownlist and to update the changes
Apr 18, 2017 08:30 AM|polachan|LINK
Please can you help in my code and I am beginner please help. I want to call public ActionResult EditCompany(Company _Company) when I click submit button, to update the changes of the company details in the viewfile.
Hi,
<div>I am a beginner . I have used dropdownlistbox and Submit button both have to httpost Action method. But I dont know how to give Httpost for dropdown and submit
Please help using my code given below
Company.cs
namespace BusinessLayer
{
public class Company
{
[Required]
public string Code { get; set; }
[Required]
public string CompanyName { get; set; }
[Required]
public string Address1 { get; set; }
}
//*******************
CompanyBL
public void EditCompany(Company _company)
{
string connectionString =
ConfigurationManager.ConnectionStrings["DataContext"].ConnectionString;
using (SqlConnection con = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand("spEditCompany", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter paramCode = new SqlParameter();
paramCode.ParameterName = "@Code";
paramCode.Value = _company.Code;
cmd.Parameters.Add(paramCode);
SqlParameter paramName = new SqlParameter();
paramName.ParameterName = "@CompanyName";
paramName.Value = _company.CompanyName;
cmd.Parameters.Add(paramName);
SqlParameter paramAddr1 = new SqlParameter();
paramAddr1.ParameterName = "@Address1";
paramAddr1.Value = _company.Address1;
cmd.Parameters.Add(paramAddr1);
cmd.Parameters.Add(paramHidden);
con.Open();
cmd.ExecuteNonQuery();
}
}
//***********************
EditCompany.cshtml
@model BusinessLayer.Company
@{
ViewBag.Title = "EditCompany";
}
<h2>EditCompany</h2>
<script src="~/Scripts/jquery-3.1.1.js"></script>
<script>
$(function ()
{
$('#Companies').change(function () {
//$('#Code').val($(this).val());
$('form').submit();
});
});
</script>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Company</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Code, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("Companies", ViewBag.Companies as SelectList, "Select Company..") //set datasource for DropDownList
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CompanyName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CompanyName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CompanyName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Address1, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Address1, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Address1, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="SaveChanges" name="btnSubmit" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
*******
HomeController
[HttpGet]
public ActionResult EditCompany()
{
CompanyBL _companybl = new CompanyBL();
IEnumerable<Company> companies = _companybl.ListCompany;
ViewBag.Companies = new SelectList(companies, "Code", "CompanyName");
return View();
}
[HttpPost]
public ActionResult EditCompany(string Companies) // This should be worked when I select company code dropdownlist
{
Company _company = new Company();
CompanyBL _companybl = new CompanyBL();
IEnumerable<Company> companies = _companybl.ListCompany;
ViewBag.Companies = new SelectList(companies, "Code", "CompanyName");
_company = _companybl.GetcompanyById(Companies);
return View(_company);
}
public ActionResult EditCompany(Company _Company) // This should be worked when I click submit button
{
if (ModelState.IsValid)
{
CompanyBL _companybl = new CompanyBL();
_companybl.EditCompany(_company);
return RedirectToAction("Index");
}
return View();
}
With many Thanks
Pol
</div>Participant
1411 Points
394 Posts
Re: how to call httpost editcompany for dropdownlist and to update the changes
Apr 18, 2017 09:35 AM|dhavalu|LINK
Read https://msdn.microsoft.com/en-us/library/dd460542(v=vs.118).aspx
You need to pass certain action and controller parameters with BeginForm helper method.
E.g
Regards
Dhaval Upadhyaya
http://dhavalupadhyaya.wordpress.com/
Contributor
5200 Points
2307 Posts
Re: how to call httpost editcompany for dropdownlist and to update the changes
Apr 19, 2017 08:59 AM|AngelinaJolie|LINK
Due to you use $('form').submit(); ,so when you select the dropdownlist and click the submit button ,it all submit the form ,
and the default @using (Html.BeginForm()) is HttpPost action way to submit form;
So I suggest that you could use anther way to achieve :
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.