开发者

No value display in view state

Problem:

I have problem to find a solution that value of txtboxMaxprice will not display if value is 0 or null

Request:

I don't want the txtboxMaxprice value to display in view state if the value is 0 or null in the input textbox.

This source code is made in a simplified version to make more understandable for end users.

// Fullmetalboy

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<BokButik1.ViewModels.SokningppPerform2ViewModel>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    PerformSearch
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>SökResultat</h2>

    <% using (Html.BeginForm("Alternativ1", "Sokning", FormMethod.Post))
    { %> 
        <table>
        <tr>
            <td>Maxprice</td>
            <td><input type="text" id="txtboxMaxprice" name="txtboxMaxprice" value="<%: Model.Maxprice %>" /></td>
            <td></td>
        </tr>
        <tr>
            <td></td>
            <td></td>
            <td><input type="submit" value="Filtrera" /></td>
        </tr>
        </table>
    <% } %>    





        //
        // Post: /Sokning/Alternativ1

        [HttpPost]
        public ActionResult Alternativ1(decimal? txtBoxMaxprice)
            {

            var SokningppPerform2ViewModel = new SokningppPerform2ViewModel()
            {

     开发者_如何学运维           Maxprice = txtBoxMaxprice)

            };


            return View("PerformSearch", SokningppPerform2ViewModel);
        }


If the value written into the text box is null then it will not display any number.

This should work for you:

int? textBoxMaxValue = (Model.MaxPrice > 0) Model.MaxPrice : null;
 <tr>
      <td>Maxprice</td>
      <td><input type="text" id="txtboxMaxprice" name="txtboxMaxprice" 
         value="<%: textBoxMaxValue %>" /></td>
      <td></td>
 </tr>

You can see in this example that if the value of textBoxMaxValue is null then nothing is written into the value attribute of the textbox.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜