1

Parameter string Temat is empty when I click button where is problem? It's look OK?

file.cshtml

    @using (Html.BeginForm())
    {
    <table class="edt" style="float: left; width: 600px; margin-top: 5px; background-color: white;">
    <tr>
                <td class="labelm">
                    @Html.LabelFor(m => m.Temat)<span id="Temat_2" class="a">*</span>
                </td>
                <td>
                    @Html.TextBoxFor(m => m.Temat,  new { id = "Temat" })
                </td>
    </tr>
    </table>
    <input type="button" value="save" onclick="Save();" />
    }

<script type="text/javascript">

    function Save() {
    var Temat = $('#Temat').val();

            $.ajax({
                url: '@Url.Action("DodajTematSave", "StronaGlowna")',
                dataType: "json",
                data: { Temat: Temat },
                type: "POST",
                async: false,
                error: function() {
                },
                success: function(data) {
                    if (data.Success) {
                        alert('success');
                    }

                }
            });
        }

</script>

JSON Method in Controller:

public JsonResult DodajTematSave(string Temat)
{
    var model = new StronaGlowna();
    if (!TryUpdateModel(model) || !ModelState.IsValid) // podładowanie yield 
    {
    }
    string chceSprobowac = Temat;
    return Json(new { Success = true});
}

1 Answer 1

2

I suppose you want to retrieve the value of the textbox. If so, assign the id 'Temat' to the text box rather than then span and your code should work fine.

But if you are interested to retrieve the text of the span then use

var Temat = $('#Temat').text();

If you want to assign id to the textbox,

@Html.TextBoxFor(model => model.Password, new { id = "Temat" })
7
  • I gave got problem with test it because @Html.LabelFor(m => m.Temat) and @Html.TextBoxFor(m => m.Temat, new { size = 47 }) have got similar id how shuld I change it for one of them? Commented Mar 24, 2013 at 10:58
  • Value is empty in textbox but I don`t know why before I get "*" this was text from span but I need text from textbox. Commented Mar 24, 2013 at 11:01
  • Just assign the id 'Temat' to the text box and remove it from the span. your code should work fine. Commented Mar 24, 2013 at 11:05
  • I check and its not work. I dont know why. I can`t get data from textBox Commented Mar 24, 2013 at 11:11
  • code looks okay now. Can you check the generated html from the browser and see if the id has been set correctly? Commented Mar 24, 2013 at 11:15

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.