简体   繁体   中英

table id is not passing from view to controller in ASP.NET MVC5

I am using asp.net mvc5 app and I have partialView which I am using for both add and delete operations and I have separate actionResult to deal each requested according that is fire by jQuery Ajax functions.... I got figure problem when I try to edit record. it pick right record and display in screen in HttpGet actionResult but when I submit record with updated field, it doesn't update that in database and is because it doesn't send recordID {PK} back to action in controller unless I use hidden value for primary key in view... however with this I can't create new record!

partial View

@model DatabaseLayer.TableMappings.FeeZone

<script>

    function CancelPage() {
        $.ajax({
            type: "POST",
            url: "/Qualification/FeeZoneHome",
            dataType: 'json',
            success: function (jsonData) {
                window.location = jsonData.redirectUrl;
            },
            error: function (error) {
            }
        });
    }   
</script>

@using (Html.BeginForm())
{
 @Html.AntiForgeryToken()

 <div class="form-horizontal">
    <h4>FeeZone</h4>
    <hr />
    @Html.ValidationSummary(true)

    <!--<div class="form-group">
        <div class="col-md-10">
            @Html.HiddenFor(model => model.FeeZoneID)
        </div>
    </div>-->

    <div class="form-group">
        @Html.LabelFor(model => model.FeeZoneDescription, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.FeeZoneDescription)
            @Html.ValidationMessageFor(model => model.FeeZoneDescription)
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Save" class="btn btn-default">
            <input type="button" value="Cancel" class="btn btn-default" onclick="CancelPage();" />
        </div>
    </div>
</div>
}

controller Method

    [HttpGet]
    public ActionResult EditFreeZoneByID(string FreeZoneID)
    {
        int numID = Convert.ToInt32(FreeZoneID);
        return PartialView("Partial_CreateNewFreeZone", Q_UOF.GetFreeZoneByID(numID));
    }

    [HttpPost]
    public ActionResult EditFreeZoneByID(FeeZone obj)
    {
        try
        {
            if (ModelState.IsValid)
            {

                Q_UOF.EditSubject(obj);

            }
        }
        catch (DataException)
        {
            ModelState.AddModelError("FeeZone", "Unable to Save Changes.");
        }

        return RedirectToAction("FreeZone");
    }

You need to send the Primary Key value from your form to your Action method so that it can update the proper record. For this you definitly need to keep the ID in the form in a hidden field.

I am not sure what you mean by "You can not create new record if you keep the hidden variable". I do not think it is going to be a problem. You can use the same HttpPost action method to save New record and update an existing record. You may simply check the value of your primary key property and If it is 0, Add a new record , else update existing record

[HttpPost]
public ActionResult EditFreeZoneByID(FeeZone obj)
{
  if(obj.FreeZoneID==0)
  {
    //This is a new record. Save it

  }
  else
  {
    //Update an existing record now. 
  }
  // to do : Return somethign valid or redirect
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM