1

Consider the following html with three submit buttons:

@using (Html.BeginForm("Process", "Part4", FormMethod.Post, new { id = "submitForm" }))
{
    <select class="form-control" name="lbToolingID" id="lbToolingID">
    @foreach (var toolingID in toolingIDList)
    {
        <option value="@toolingID">@toolingID</option>
    }
    <input class="btn btn-primary" type="submit" name="btnAction" value="Add Tooling" />

    @* Selected tool will be added here on the [Add Tooling] button click *@
    <table>
    @if (Model != null && Model.M_PartTooling_List != null && Model.M_PartTooling_List.Count > 0)
    {
        for (int i = 0; i < Model.M_PartTooling_List.Count; i++)
        {
            <tr>
                <td nowrap>
                    @Html.ActionLink("Remove Location", "Remove", new { @id = Model.M_PartTooling_List[@i].ID }, new { @class = "btn-sm btn-danger" })
                </td>
                <td nowrap colspan="2">
                    @Html.HiddenFor(model => model.M_PartTooling_List[i].ToolingID)
                    @(Model.M_PartTooling_List[i].ToolingID)
                </td>
            </tr>
        }
    } 
    </table>

    @* Once done these buttons will add the records into database *@
    <input class="btn btn-success" type="submit" name="btnAction" value="Save & Return To List" />
    <input class="btn btn-success" type="submit" name="btnAction" value="Save & Continue Create" />
}
...
...
@section Scripts 
{
    @Scripts.Render("~/bundles/jqueryval")
}

The following is the action :

[HttpPost]
public ActionResult Process(M_PartViewModels m_PartViewModels, string btnAction, string lbToolingID)
{
    if (btnAction == "Add Tooling")
    {
        AddPartTooling(m_PartViewModels, lbToolingID);

        return View("Create", m_PartViewModels);
    }

    CreatePart(m_PartViewModels);

    if (btnAction == "Save & Return To List")
    {
        return RedirectToAction("Index");
    }

    return RedirectToAction("Create");
}

On the first time clicking the Add Tooling button, it is working fine, we can see that on breakpoint the value of btnAction is indeed Add Tooling :

enter image description here

But on the second time clicking the Add Tooling button or any other two buttons, somehow the value of btnAction became null :

enter image description here

We have already inspect the said button in the browser and we can see that value of the button still intact :

enter image description here

Can somebody point out what was the cause of this and how to work around this?

I know we can work around this using jQuery but we wanted try this with minimal jQuery coding if possible.

UPDATE

Removing the reference to this script got the page to work as intended but it disables any other (remote) validations that we had:

@section Scripts 
{
    @Scripts.Render("~/bundles/jqueryval")
}

Will look more into this. Possibly if we can disable the call to remote validation temporarily on Add Location button click.

UPDATE 2017-06-22 0017 WITH OK SOLUTION - jquery.validate* is the culprit?

This may not be the solution I would like it to be but as long as it is working I am OK with this.

Since we already knew that removing the

@Scripts.Render("~/bundles/jqueryval")

made the page work as intended.

This is the bundle definition by the way :

bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include("~/Scripts/jquery.validate*")); 

I decided to ditch the remote validation

[Remote("CheckPartID", "Part", AdditionalFields = "PartID_Ori", ErrorMessage = "Part ID already in use!")]

defined in my model annotation and port the validation into my action instead :

    [HttpPost]
    public ActionResult CreateProcess(M_PartViewModels m_PartViewModels, string btnAction, string lbToolingID)
    {
        ViewBag.VMainMenu = GetMenu();
        ViewBag.ToolingIDList = GetToolingID();

        CheckPartID(m_PartViewModels);
        CheckPartDesc(m_PartViewModels); 

        if (btnAction == "Add Tooling")
        {
            AddPartTooling(m_PartViewModels, lbToolingID);

            return View("Create", m_PartViewModels);
        } 

        if (ModelState.IsValid)
        { 
            // Do some DB insert stuff

            if (btnAction == "Save & Return To List")
            {
                return RedirectToAction("Index");
            }
            else
            {
                return RedirectToAction("Create");
            } 
        }

        return View("Create", m_PartViewModels);
    } 

    public void CheckPartID(M_PartViewModels m_PartViewModels)
    {
        if (m_PartViewModels.M_Part.PartID != m_PartViewModels.M_Part.PartID_Ori)
        {
            var routingID = db.M_Part.Where(u => u.PartID == m_PartViewModels.M_Part.PartID).FirstOrDefault();

            if (routingID != null)
            {
                ModelState.AddModelError("M_Part.PartID", "Part ID already exists."); 
            }
        }
    }

    public void CheckPartDesc(M_PartViewModels m_PartViewModels)
    {
        if (m_PartViewModels.M_Part.PartDesc != m_PartViewModels.M_Part.PartDesc_Ori)
        {
            var routingID = db.M_Part.Where(u => u.PartDesc == m_PartViewModels.M_Part.PartDesc).FirstOrDefault();

            if (routingID != null)
            {
                ModelState.AddModelError("M_Part.PartDesc", "Part Description already exists.");
            }
        } 
    }

While I felt not entirely satisfied with this solution (where the validation only fires on submit instead on keyup) but since it is working as intended will use this for now.

Will update this once found better way.

Thank you for your kind attention guys. I really appreciate it. :)

11
  • Look at the request payload in the network pane of Google inspect to see what you get. Commented Jun 21, 2017 at 0:19
  • Hi @W.Scott, already looked at it. Can you advice what should I be seeing here? Inspecting the request payload is a new thing for me. Sorry for the trouble. Commented Jun 21, 2017 at 0:35
  • You should see a screen like this: i.sstatic.net/2NPRm.png you can hit "View Parsed " and see exactly what was sent to the server. Post that here too. Commented Jun 21, 2017 at 1:24
  • The following is on the first Add Tooling button : [link] (i.sstatic.net/tilJd.png). And this is the second click : [;ink] (i.sstatic.net/vIAJU.png). Updated the question in case you are wondering where the M_PartTooling_List[0].ToolingID came from. Currently the action for the @Html.ActionLink("Remove Location", "Remove", new { @id = Model.M_PartTooling_List[@i].ID }, new { @class = "btn-sm btn-danger" }) not yet created. I tried removing this before but the same irregularity occurs. Commented Jun 21, 2017 at 1:59
  • 1
    Is there any javascript associated with this view? Commented Jun 21, 2017 at 2:49

0

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.